简体   繁体   English

在R中的循环内创建向量

[英]Create a vector within a Loop in R

I have the following code: 我有以下代码:

    generator <- function(n){
  nodes <- c()
  distances <- c()
  for (main in 1:n){
    for (i in 1:n) {
      for (j in 1:n){
        if (main != i & i != j & main != j & i < j){
          nodes <- c(nodes, paste(main, i, j, sep=",", collapse=""))
          distances <- c(distances, main+i+j)}}}}
  data <- data.frame(nodes, distances)
  return(data)}

Running generator(4), I get the following output: 运行generator(4),我得到以下输出:

      nodes distances
1  1,2,3         6
2  1,2,4         7
3  1,3,4         8
4  2,1,3         6
5  2,1,4         7
6  2,3,4         9
7  3,1,2         6
8  3,1,4         8
9  3,2,4         9
10 4,1,2         7
11 4,1,3         8
12 4,2,3         9

What I would like is to have the items in the "nodes" column be actual vectors of values, with the goal of comparing different triplets of nodes and finding the common members. 我想让“节点”列中的项成为值的实际向量,目的是比较节点的不同三元组并查找公共成员。 It is currently just a string. 目前它只是一个字符串。 So, for instance, I would like a$nodes[1][1] , to yield 1 , or something along those lines, so I can extract every individual value from the nodes triplets. 因此,例如,我希望a$nodes[1][1]产生1或类似的东西,这样我就可以从nodes三元组中提取每个单独的值。

I currently have paste(main, i, j, sep=",") and I tried replacing this with c(main, i, j) but what happened is that I got a data frame of 36 rows instead of twelve: each individual node was its own row. 我目前有paste(main, i, j, sep=",") ,我尝试用c(main, i, j)替换它,但是发生的是,我得到了36行而不是十二行的数据框:每个人节点是它自己的行。

Thank you. 谢谢。

You need to work with list columns to do what you want to do. 您需要使用列表列来做您想做的事情。

I suggest working with tibbles instead of regular data frames, they're much more convenient for this type of work. 我建议使用小标题而不是常规数据帧,它们对于这种类型的工作更加方便。

If you'd rather stick with base R you can replace data <- tibble::tibble(nodes, distances) by: 如果您想使用基数R ,则可以将data <- tibble::tibble(nodes, distances)替换为:

data <- data.frame(distances)
data$nodes <- nodes  

new function 新功能

generator <- function(n){
  nodes <- list()      # changed this from `c()` to `list()`
  distances <- c()
  for (main in 1:n){
    for (i in 1:n) {
      for (j in 1:n){
        if (main != i & i != j & main != j & i < j){
          nodes <- append(nodes, list(c(main, i, j))) # changed this to append a list instead of a vector
          distances <- c(distances, main+i+j)}}}}
  data <- tibble::tibble(nodes, distances) # changed `data.frame` to `tibble`
  return(data)}

output 输出

df <- generator(4)

df
# A tibble: 12 x 2
#        nodes distances
#      <list>     <int>
#  1 <int [3]>         6
#  2 <int [3]>         7
#  3 <int [3]>         8
#  4 <int [3]>         6
#  5 <int [3]>         7
#  6 <int [3]>         9
#  7 <int [3]>         6
#  8 <int [3]>         8
#  9 <int [3]>         9
# 10 <int [3]>         7
# 11 <int [3]>         8
# 12 <int [3]>         9

df$nodes[[1]]
# [1] 1 2 3

side note 边注

You're growing a list, which can be slow, if you run into performance issues try defining nodes with nodes <- vector("list", length = max_length) and trim it in the end with nodes <- nodes[lengths(nodes)!=0] . 您正在增加一个列表,该列表可能会很慢,如果遇到性能问题,请尝试定义nodes nodes <- vector("list", length = max_length) nodes <- nodes[lengths(nodes)!=0] ,最后用nodes <- nodes[lengths(nodes)!=0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM