使用R按列追加数据

使用R按列追加数据,r,R,我在R中运行一个循环,每次运行都会给我一个带有两个变量的数据帧——时间戳和降雨量(如下所示) 循环运行500次,我想以下面的格式创建一个文本文件- Timestamp,Rainfall_Region 1, Rainfall_Region 2,...... 01/01/2007 00:15,0.01,0.03,......... 01/01/2007 00:30,0.04,0.06,......... --------------------------

我在R中运行一个循环,每次运行都会给我一个带有两个变量的数据帧——时间戳和降雨量(如下所示)

循环运行500次,我想以下面的格式创建一个文本文件-

    Timestamp,Rainfall_Region 1, Rainfall_Region 2,...... 
    01/01/2007 00:15,0.01,0.03,.........
    01/01/2007 00:30,0.04,0.06,.........
    --------------------------
    --------------------------
记录的总数超过一百万,我无法使用cbind在R中创建一个大数据帧,然后将其导出。有没有一种方法可以在R或其他方式中实现这一点?时间戳是所有数据帧中的公共变量。任何帮助都将不胜感激。多谢各位

这是一个玩具示例:

# create a data frame with the number of rows equal to the number of
# index iteration (in this case 3). The variable must be of the same type
# of your iteration output.
df <- data.frame(x = 1:3, y = rep("some", 3), stringsAsFactors = FALSE)
some_text <- c("dog", "cat", "duck")

for (i in 1:3) {
  x <-  i + 10
  y <-  some_text[i]
  iteration_output <- data.frame(x, y, stringsAsFactors = FALSE)
  # replace each variable of the given index row from the iteration output
  df[i, "x"] <- iteration_output$x
  df[i, "y"] <- iteration_output$y
}

df
#创建行数等于行数的数据帧
#索引迭代(在本例中为3)。变量的类型必须相同
#迭代输出的一部分。

df使用与总迭代次数相同的行数创建数据帧。同样使用相同数量的变量和类型,然后用计算数据帧替换每次迭代中的行
df[iteration_index,]在循环的每次运行中,仅仅添加降雨区域作为列值怎么样?然后,您的数据是标准的“长”格式,如果需要使用
tidyr::spread
,您可以轻松地转换为“宽”。如果您有多个文件,您可以将它们一起
rbind()
# create a data frame with the number of rows equal to the number of
# index iteration (in this case 3). The variable must be of the same type
# of your iteration output.
df <- data.frame(x = 1:3, y = rep("some", 3), stringsAsFactors = FALSE)
some_text <- c("dog", "cat", "duck")

for (i in 1:3) {
  x <-  i + 10
  y <-  some_text[i]
  iteration_output <- data.frame(x, y, stringsAsFactors = FALSE)
  # replace each variable of the given index row from the iteration output
  df[i, "x"] <- iteration_output$x
  df[i, "y"] <- iteration_output$y
}

df