【问题标题】:How to consolidate multiple data series in R如何在 R 中合并多个数据系列
【发布时间】:2011-06-17 10:47:03
【问题描述】:

我正在使用 R 对实验数据进行一些统计处理。

我有多个文件,每个文件都具有相同的结构。每个文件的每一行都有针对不同日期同时进行的测量,因此一般结构如下:

time C1 C2 C3
19:00 200 10.0 30
19:01 220 10.0 45
...

我需要创建一个文件,其中包含来自多个文件的一列的值的摘要,因此我将拥有例如 C2 的平均值和标准差,在每个时间,连续几天。

time avg dev
19:00 205.0 30.0
19:01 220.0 10.0
...

【问题讨论】:

    标签: r statistics


    【解决方案1】:

    Stack Overflow 中有许多问题可以帮助您。尝试使用“[r] 多个文件”进行搜索(省略引号)。 [r] 将搜索限制为仅标记为 r 的问题。

    Here's a question 可能会满足您的需求

    here's an example的搜索

    【讨论】:

      【解决方案2】:
      library(plyr)    
      # Combine all the data
          data=rbind(data1,data2,data3)
      
          # to get the mean
          ddply(data,.(time),numcolwise(mean))
          # to get the sd
          ddply(data,.(time),numcolwise(sd))
      
          # You can combine both statements above into a single call and put the output into a data frame
          resulting_data=data.frame(ddply(data,.(time),numcolwise(mean)),ddply(data,.(time),numcolwise(sd))[,-1])
      
          # depending on the number of columns you have, name the output accordingly. For your example
          names(resulting_data)c=('time','C1'..)
      

      【讨论】:

        【解决方案3】:

        创建Files,一个文件名向量,假设文件名是指定的形式或其他形式。然后读入这些文件,将read.table 应用于每个名称并将结果绑定在一起,得到m,其中包含所有表的所有行。最后是aggregatem 数据框。

        Files <- Sys.glob("test_*.txt")
        m <- do.call(rbind, lapply(Files, read.table, header = TRUE))
        aggregate(m[-1], m[1], function(x) c(mean = mean(x), sd = sd(x)))
        

        【讨论】:

        • 谢谢,这似乎是正确的方法。然而,R 抱怨说“'FUN' 必须总是返回一个标量”。如果我使用 aggregate(m[-1].m[1],function(x) c(mean=mean(x))) 它会做平均值。
        • @pablochchacin,将 R 升级到最新版本。
        猜你喜欢
        • 1970-01-01
        • 2020-02-17
        • 2021-12-27
        • 2017-01-15
        • 1970-01-01
        • 2014-08-18
        • 2013-11-13
        • 2020-09-17
        • 2021-02-28
        相关资源
        最近更新 更多