【问题标题】:Appending data column wise using R使用 R 明智地附加数据列
【发布时间】:2017-07-15 03:38:46
【问题描述】:

我在 R 中运行一个循环,每次运行都会给我一个包含两个变量的数据框 - 时间戳和降雨(如下所示)。

    Timestamp,Rainfall_Region 1  
    01/01/2007 00:15,0.01
    01/01/2007 00:30,0.04
    --------------------------
    --------------------------

循环运行 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 或其他方式中做到这一点?时间戳是所有数据帧之间的公共变量。任何帮助将不胜感激。谢谢你。

【问题讨论】:

  • 创建一个行数与总迭代次数相同的数据框。同样具有相同数量的变量和类型,然后将每次迭代中的行替换为您的计算数据框。 df[iteration_index, ] <- iteration_output.
  • 在每次循环运行中添加 Rainfall Region 作为列值怎么样?然后您的数据是标准的“长”格式,如果您需要使用tidyr::spread,您可以轻松转换为“宽”。如果你有多个文件,你可以rbind()他们一起。

标签: r


【解决方案1】:

这是一个玩具示例:

# 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

【讨论】:

    猜你喜欢
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多