使用cast()或ddply()总结一个数据帧中两个连续变量的平均值

时间:2018-12-13 22:55:03

标签: r dataframe casting reshape plyr

数据(下面)有两个包含因子的名为“日期”和“独立变量(IV)”的列,还有两个包含连续值的名为“ Independent_value”和“ Sapflow”的额外列。

列说明:

日期= 5个月(6月至10月)内独立变量的测量值。

独立变量= 3个独立变量(即温度,湿度和辐射)。

独立值=表示从6月到10月的每天时间段内的温度,辐射和湿度的读数。

树液的流量(因变量)=从6月到10月的每天时间记录的树种树液的流量,以及自变量如何影响这些树液的流量。

目标

在这种情况下,我想使用 cast() 按组(即日期和自变量)汇总数据(在下面找到) ddply() 生成一个新的数据框,该数据框显示每月每个自变量(温度,湿度和辐射)的平均记录值以及每月自变量的平均树胶流量率格式如下:-

*键

* IV =自变量(即温度,湿度和辐射)

* Mean_IV =自变量的平均值

* Mean_Sapflow(因变量)=每个自变量每月的平均Sapflow率

 Date      IV           Mean_IV   Mean_Sapflow
1 June     Humidity     19.67     14.97
2 June     Humidity     18.82     16.31
3 June     Humidity     20.38     17.52
4 June     Humidity     14.94      7.45
5 June     Humidity     12.92     12.18
6 June     Humidity     15.28     15.82

问题:

我尝试使用 ddply() cast(),但无法生成上面显示的数据框格式。如果有人可以提供帮助,我将非常感激。

* ddply

library(plyr)

summarised_Sapflow<-ddply(Sapflow_new, c("Date", "Independent_Variable"), summarise,
                      N=length(Independent_Value),
                    mean("Independent Value","Sapflow"))

输出是一系列警告:

  Warning messages:
  1: In mean.default("Independent Value", "Sapflow") :
  argument is not numeric or logical: returning NA
  2: In mean.default("Independent Value", "Sapflow") :
  argument is not numeric or logical: returning NA
  3: In mean.default("Independent Value", "Sapflow") :
  argument is not numeric or logical: returning NA

cast()

library(reshape)

Sapflow.Summary<-cast(Sapflow_new, 
                 Date~Independent_Variable, mean, 
                 value=c('Independent_Value','Sapflow'))

此输出非常接近我的目标,但是由于没有正确的代码排列,因此意味着缺少sapflow,并且“日期”中包含的月份的排列顺序错误。

           Date       Humidity Radiation Temperature
         1 August     18.38968  178.9806    71.73355
         2 July       21.80065  270.9065    61.33065
         3 June       17.60733  263.6733    70.56133
         4 October    11.34867   93.6000    81.74300
         5 September  14.82200  152.2333    72.21367

数据:

structure(list(Date = structure(c(3L, 3L, 3L, 3L, 3L, 
3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 
1L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 
5L, 5L, 5L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 
4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L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.02, 21.69, 18.03, 
18.46, 
18.45, 20.78, 23.04, 22.05, 19.71, 20.59, 24.89, 23.34, 
24.7, 
24.2, 22.43, 18.21, 17.66, 18.23, 20.36, 22.83, 23.52, 
22.88, 
19.59, 21.51, 22.25, 21.47, 22.03, 22.51, 25.54, 24.01, 
24.28, 
26.21, 23.72, 17.63, 17.27, 19.19, 19.97, 19.84, 22.78, 
24.46, 
23.05, 23.31, 24.75, 23.23, 18.91, 15.56, 13.51, 15.8, 
17.67, 
19.18, 18.93, 20.05, 17.1, 16.87, 18.77, 20.49, 21.5, 
18.04, 
18.82, 17.38, 13.05, 13.13, 13.48, 16.32, 16.74, 16.11, 
15.77, 
15.48, 18.17, 18.16, 18.44, 16.63, 16.64, 14.47, 13.07, 
14.14, 
 17.27, 16.71, 18.22, 12.9, 13.95, 14.7, 15.78, 17.52, 
19.66, 
18.87, 18.07, 16.4, 12.92, 10.57, 10.04, 9.78, 10.24, 
 14.25, 
15.92, 11.59, 9.25, 10.33, 11.22, 15.03, 13.67, 14.26, 
15.42, 
8.34, 8.56, 12.37, 14.38, 15.47, 16.4, 17.15, 20.05, 
11.08, 10.63, 
14.34, 13.27, 9.33, 8.1, 10.95, 12.79, 8.64, 11.42, 
12.12, 9.91, 
7.86, 3.51, 4.97, 3.63, 5.59, 85.07, 79.72, 72.83, 90.1, 
83.02, 
 73.34, 77.11, 74.79, 81.66, 77.71, 66.14, 78.15, 69.33, 
68.13, 
60.31, 69.47, 81.86, 78.63, 77.69, 77.56, 52.88, 53.32, 
53.74, 
55.85, 49.56, 55.3, 69.25, 74.96, 69.29, 60.07, 54.31, 
48.6, 
55.73, 56.74, 47.66, 60.51, 55.64, 58.39, 63.8, 63.16, 
73.65, 
71.08, 64.34, 60.1, 51.61, 54.87, 58.23, 52.49, 52.56, 
59.64, 
67.85, 64.42, 60.08, 59.71, 57.12, 58.7, 68.85, 72.44, 
89.13, 
77.67, 62.17, 61.3, 63.58, 66.26, 60.09, 56.63, 53.11, 
59.84, 
60.06, 80.76, 79.51, 73.96, 84.58, 78.77, 71.65, 72.59, 
77.52, 
69.04, 78.26, 77.22, 73.75, 81.95, 82.04, 78.14, 73.41, 
72.76, 
90.68, 74.24, 71.3, 74.4, 60.26, 66.08, 65.18, 57.17, 
66.88, 
75.53, 71.52, 74.97, 66.02, 78.06, 73.58, 68.18, 83.55, 
80.4, 
66.28, 72.32, 72.39, 77.74, 69.81, 74.21, 77.37, 88.28, 
65.33, 
87.54, 80.49, 69.58, 68.18, 69.25, 60.06, 66.38, 68.51, 
71.65, 
63.29, 76.63, 80.46, 85.56, 81.25, 94.48, 73.87, 76.8, 
72.83, 
77.55, 81.5, 77.7, 75.79, 94.38, 99.55, 94.14, 87.29, 
84.81, 
82.63, 85.27, 84.52, 71.13, 76.28, 78.06, 82.83, 75.18, 
83.8, 
85.38, 84, 85.33, 197.8, 195.5, 288, 72, 160.5, 337.1, 
176.9, 
242.3, 189.4, 295.7, 363.2, 158, 290, 251.2, 297.3, 
192.6, 163.5, 
274.5, 210.7, 243.4, 287.4, 375.7, 290.5, 336.4, 361.6, 
369.2, 
 302.6, 295.2, 348.5, 343.5, 327.6, 358.9, 358.6, 288.9, 
 325.6, 
307.8, 321.3, 321.5, 280.6, 264.9, 253, 279.5, 318.1, 
285.1, 
330.8, 252, 201, 229.9, 259.3, 230.4, 265.5, 214.1, 307, 
311.1, 
282.5, 256.9, 227.2, 263.4, 68.2, 130.8, 276.6, 299.2, 
276.5, 
243.9, 291, 289.3, 290.6, 259.6, 220.5, 72.7, 158.9, 
233.8, 105.9, 
164.2, 168.1, 188.7, 120.1, 217.7, 111.2, 114.7, 143.6, 
55.2, 
108.5, 162.2, 185, 197.7, 54.1, 126.3, 111.2, 135.4, 
228.3, 214.3, 
 240.1, 247.6, 173, 172.4, 131.9, 149.4, 203.1, 92.3, 
168.5, 146.6, 
65.9, 103.6, 200.2, 131.3, 183.5, 128.3, 140.6, 124.1, 
125.9, 
75.8, 173.2, 47.9, 111.7, 205.8, 188.3, 175.6, 193.7, 
170.4, 
188.3, 108, 171.1, 59.5, 87.7, 142.2, 111.8, 26.3, 
129.9, 103.1, 
158.7, 147.9, 109.8, 67.8, 106.6, 12.3, 15.8, 53, 63.4, 
86.2, 
123.3, 112.9, 128.2, 141.9, 81.6, 102, 86.8, 83.9, 50, 
96.8, 
100.5, 47), Sapflow = c(14.97, 16.31, 17.52, 7.45, 
 12.18, 15.82, 
 11.79, 14.45, 10.95, 13.62, 16.28, 11.42, 16.13, 15.09, 
 17.28, 
 14.43, 11.7, 16.06, 17.66, 16.33, 17.79, 18.58, 19.41, 
 19.8, 
 21.63, 21.35, 17.81, 17.56, 19.37, 21.27, 23.26, 23.67, 
  22.64, 
 21.85, 24.81, 22.36, 24.72, 23.87, 23.67, 22.01, 19.23, 
 19.92, 
 21.99, 23.6, 24.9, 24.46, 22.22, 23.95, 24.81, 23.88, 
 22.98, 
 24.47, 26.09, 25.97, 25.82, 26.24, 25.09, 22, 16.91, 
 21.35, 25.32, 
  25.76, 26.38, 25.78, 25.77, 25.15, 26.29, 26.22, 
  24.59, 18.26, 
 18.91, 21.57, 21.37, 21.29, 23.96, 24.85, 21.02, 23.05, 
 22.69, 
 23.9, 25.24, 25.4, 23.19, 22.8, 22.08, 21.86, 13.82, 
 22.05, 23.21, 
 20.12, 22.73, 21.88, 23.33, 24.76, 23.5, 22.06, 22.01, 
  20.65, 
 21.54, 19.9, 21.67, 21.84, 18.82, 17.99, 21.41, 23.53, 
 23.39, 
 25.75, 22.62, 22.25, 21.81, 16.81, 20.42, 12.08, 12.36, 
  15.31, 
 14.14, 15.48, 15.18, 14.19, 12.09, 12.39, 12.34, 12.61, 
 10.79, 
 10.53, 11.29, 9.92, 9.79, 10.86, 10.98, 10.58, 12.54, 
 12.52, 
 12.25, 6.38, 0.91, 5.24, 6.56, 5.72, 4.55, 4.99, 2.88, 
 0.99, 
 1.03, 1.57, 2.07, 2.3, 2.22, 2.11, 2.21, 2.29, 14.97, 
 16.31, 
 17.52, 7.45, 12.18, 15.82, 11.79, 14.45, 10.95, 13.62, 
 16.28, 
 11.42, 16.13, 15.09, 17.28, 14.43, 11.7, 16.06, 17.66, 
  16.33, 
 17.79, 18.58, 19.41, 19.8, 21.63, 21.35, 17.81, 17.56, 
 19.37, 
  21.27, 23.26, 23.67, 22.64, 21.85, 24.81, 22.36, 
  24.72, 23.87, 
23.67, 22.01, 19.23, 19.92, 21.99, 23.6, 24.9, 24.46, 
22.22, 
23.95, 24.81, 23.88, 22.98, 24.47, 26.09, 25.97, 25.82, 
26.24, 
25.09, 22, 16.91, 21.35, 25.32, 25.76, 26.38, 25.78, 
25.77, 25.15, 
26.29, 26.22, 24.59, 18.26, 18.91, 21.57, 21.37, 21.29, 
23.96, 
24.85, 21.02, 23.05, 22.69, 23.9, 25.24, 25.4, 23.19, 
22.8, 22.08, 
21.86, 13.82, 22.05, 23.21, 20.12, 22.73, 21.88, 23.33, 
24.76, 
23.5, 22.06, 22.01, 20.65, 21.54, 19.9, 21.67, 21.84, 
18.82, 
17.99, 21.41, 23.53, 23.39, 25.75, 22.62, 22.25, 21.81, 
16.81, 
20.42, 12.08, 12.36, 15.31, 14.14, 15.48, 15.18, 14.19, 
12.09, 
12.39, 12.34, 12.61, 10.79, 10.53, 11.29, 9.92, 9.79, 
 10.86, 
10.98, 10.58, 12.54, 12.52, 12.25, 6.38, 0.91, 5.24, 
6.56, 5.72, 
4.55, 4.99, 2.88, 0.99, 1.03, 1.57, 2.07, 2.3, 2.22, 
 2.11, 2.21, 
 2.29, 14.97, 16.31, 17.52, 7.45, 12.18, 15.82, 11.79, 
14.45, 
 10.95, 13.62, 16.28, 11.42, 16.13, 15.09, 17.28, 14.43, 
 11.7, 
 16.06, 17.66, 16.33, 17.79, 18.58, 19.41, 19.8, 21.63, 
 21.35, 
 17.81, 17.56, 19.37, 21.27, 23.26, 23.67, 22.64, 21.85, 
 24.81, 
 22.36, 24.72, 23.87, 23.67, 22.01, 19.23, 19.92, 21.99, 
 23.6, 
 24.9, 24.46, 22.22, 23.95, 24.81, 23.88, 22.98, 24.47, 
 26.09, 
 25.97, 25.82, 26.24, 25.09, 22, 16.91, 21.35, 25.32, 
 25.76, 26.38, 
 25.78, 25.77, 25.15, 26.29, 26.22, 24.59, 18.26, 18.91, 
 21.57, 
 21.37, 21.29, 23.96, 24.85, 21.02, 23.05, 22.69, 23.9, 
 25.24, 
 25.4, 23.19, 22.8, 22.08, 21.86, 13.82, 22.05, 23.21, 
 20.12, 
 22.73, 21.88, 23.33, 24.76, 23.5, 22.06, 22.01, 20.65, 
 21.54, 
 19.9, 21.67, 21.84, 18.82, 17.99, 21.41, 23.53, 23.39, 
 25.75, 
 22.62, 22.25, 21.81, 16.81, 20.42, 12.08, 12.36, 15.31, 
 14.14, 
 15.48, 15.18, 14.19, 12.09, 12.39, 12.34, 12.61, 10.79, 
 10.53, 
 11.29, 9.92, 9.79, 10.86, 10.98, 10.58, 12.54, 12.52, 
 12.25, 
 6.38, 0.91, 5.24, 6.56, 5.72, 4.55, 4.99, 2.88, 0.99, 
 1.03, 1.57, 
 2.07, 2.3, 2.22, 2.11, 2.21, 2.29)), class = 
 "data.frame", row.names = c(NA, 
 -456L))

1 个答案:

答案 0 :(得分:1)

这不是ddply()cast()解决方案,但可以使用tidyversereshape2来做到:

df %>%
 group_by(Date, Independent_Variable) %>%
 summarise(Independent_Value = mean(Independent_Value)) %>%
 mutate(Independent_Variable = paste(Independent_Variable, "IV", sep = "_")) %>%
 dcast(Date~Independent_Variable, value.var = "Independent_Value") %>%
 arrange(factor(Date, levels = month.name)) %>%
 left_join(df %>%
 group_by(Date, Independent_Variable) %>%
 summarise(Sapflow = mean(Sapflow)) %>%
 mutate(Independent_Variable = paste(Independent_Variable, "Sapflow", sep = "_")) %>%
 dcast(Date~Independent_Variable, value.var = "Sapflow") %>%
 arrange(factor(Date, levels = month.name)),
 by = c("Date" = "Date"))

       Date Humidity_IV Radiation_IV Temperature_IV Humidity_Sapflow
1      June    17.60733     263.6733       70.56133        16.067000
2      July    21.80065     270.9065       61.33065        23.356774
3    August    18.38968     178.9806       71.73355        22.941613
4 September    14.82200     152.2333       72.21367        19.309333
5   October    11.34867      93.6000       81.74300         6.700667
  Radiation_Sapflow Temperature_Sapflow
1         16.067000           16.067000
2         23.356774           23.356774
3         22.941613           22.941613
4         19.309333           19.309333
5          6.700667            6.700667

首先,它按“日期”和“ Independent_Variable”分组并汇总“ Independent_Value”。其次,它将“ _IV”添加到Independent_Variable中的值。第三,它正在重塑数据并按照几个月的真实顺序进行安排。第四,它正在执行“ Sapflow”的前三个步骤。最后,它将两者合并。

或者仅使用tidyverse

df %>%
 group_by(Date, Independent_Variable) %>% #Grouping
 summarise_all(funs(mean = mean(.))) %>% #Summarising all variables and adding "_mean" to the new variables
 arrange(factor(Date, levels = month.name)) #Arranging according the real order of months

   Date      Independent_Variable Independent_Value_mean Sapflow_mean
   <fct>     <fct>                                 <dbl>        <dbl>
 1 June      Humidity                               17.6        16.1 
 2 June      Radiation                             264.         16.1 
 3 June      Temperature                            70.6        16.1 
 4 July      Humidity                               21.8        23.4 
 5 July      Radiation                             271.         23.4 
 6 July      Temperature                            61.3        23.4