【问题标题】:concatenate data frame by column by month in R在R中按月逐列连接数据帧
【发布时间】:2014-10-24 11:12:17
【问题描述】:

我想将 2 个数据框按列组合成一个。我的数据看起来像:

     station build   Year Month  
    1   Bariko 24.5  1961     1      
    2   Bariko 29.1  1962     1       
    3   Bariko 26.4  1963     1       
    4   Bariko 29.0  1961     2       
    5   Bariko 22.0  1962     2      
    6   Bariko 25.9  1963     2       
    7   Bariko 24.2  1961     3       
    8   Bariko 23.9  1962     3       
    9   Bariko 24.4  1963     3       
    10  Bariko 24.0  1961     4      
    11  Bariko 24.2  1962     4      
    12  Bariko 24.8  1963     4      

我按月计算构建的平均值。我可以用“平均”来命名。

    newdata=aggregate(build ~ station +Month, data=data, mean, na.rm = TRUE) 

1)我想结合“数据”和“新数据”(4 行对应于我的 4 个月),以获得 12 行和 5 列的数据框(我可以重复每个月的平均值)。

2) 根据第 2 列和第 5 列之间的差异(data$build-data$mean)创建一个新的 value 列。最后,我将获得 6 列的数据(站、构建、年、月、平均值、差异)。最后,我可以按年份绘制差异。

谢谢,

【问题讨论】:

  • ave 可以让这更容易dat$mn <- with(dat, ave(build , list(station, Month), FUN=mean) )

标签: r plyr


【解决方案1】:

用dplyr试试这个:

station = "Bariko"
build = c(24.5, 29.1, 26.4, 29, 22, 25.9, 24.2, 23.9, 24.4, 24, 24.2, 24.8)
Year = c(1961,1962,1963)
Month = rep(1:4, each=3)
df<-data.frame(station,build,Year,Month)
df

dplyr 让这些事情变得非常简单:

library(dplyr)

df %>%
  group_by(Month) %>%
  mutate(mean=mean(build)) %>%
  mutate (diff= build-mean)



   station build Year Month     mean        diff
1   Bariko  24.5 1961     1 26.66667 -2.16666667
2   Bariko  29.1 1962     1 26.66667  2.43333333
3   Bariko  26.4 1963     1 26.66667 -0.26666667
4   Bariko  29.0 1961     2 25.63333  3.36666667
5   Bariko  22.0 1962     2 25.63333 -3.63333333
6   Bariko  25.9 1963     2 25.63333  0.26666667
7   Bariko  24.2 1961     3 24.16667  0.03333333
8   Bariko  23.9 1962     3 24.16667 -0.26666667
9   Bariko  24.4 1963     3 24.16667  0.23333333
10  Bariko  24.0 1961     4 24.33333 -0.33333333
11  Bariko  24.2 1962     4 24.33333 -0.13333333
12  Bariko  24.8 1963     4 24.33333  0.46666667

【讨论】:

    【解决方案2】:

    为了演示基础 R 中的替代方案(结合 @user20650 在 cmets 中建议的元素),within + ave 将在这里成为一个优秀的团队。 ave可以带不同的函数,但默认是mean,所以不需要指定聚合函数。

    within(df, {
      Mean <- ave(build, station, Month)
      Diff <- Mean - build
    })
    #    station build Year Month        Diff     Mean
    # 1   Bariko  24.5 1961     1  2.16666667 26.66667
    # 2   Bariko  29.1 1962     1 -2.43333333 26.66667
    # 3   Bariko  26.4 1963     1  0.26666667 26.66667
    # 4   Bariko  29.0 1961     2 -3.36666667 25.63333
    # 5   Bariko  22.0 1962     2  3.63333333 25.63333
    # 6   Bariko  25.9 1963     2 -0.26666667 25.63333
    # 7   Bariko  24.2 1961     3 -0.03333333 24.16667
    # 8   Bariko  23.9 1962     3  0.26666667 24.16667
    # 9   Bariko  24.4 1963     3 -0.23333333 24.16667
    # 10  Bariko  24.0 1961     4  0.33333333 24.33333
    # 11  Bariko  24.2 1962     4  0.13333333 24.33333
    # 12  Bariko  24.8 1963     4 -0.46666667 24.33333
    

    【讨论】:

      【解决方案3】:

      使用data.table 包:

      library(data.table)
      dt <- data.table(df)
      dt[, Mean := mean(build), by = Month]
      dt[, Diff := Mean - build]
      

      使用plyr 包:

      library(plyr)
      df <- ddply(df, "Month", transform, Mean = mean(build))
      df$Diff = with(df, Mean - build)
      

      【讨论】:

      • @510947,如果您想建议对此答案进行改进,请在 cmets 中进行,而不是强行将其纳入答案本身
      • 好吧..那么你去 - 对于你的第二个例子,你可以使用 mutate,这将为你节省一个步骤,就像这样 ddply(df,.(Month),mutate,mean=mean(build),diff=build-mean)
      猜你喜欢
      • 2022-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多