2016-07-28 57 views
1

我最近发布了一个question关于使用dplyr函数group_bysummarise突然出现的错误。 错误的原因是由于在数据帧中有一个列矩阵而不是常规矢量。 这是通过将矩阵强制转换为矢量来解决的......但是,我现在想知道的是它是如何到达那里的!矩阵出现在数据框内 - 这是怎么发生的?

我的工作可以用下面的代码是downloaded here并准备向使用DataSet:

library(pracma) 
library(plyr) 
library(dplyr) 

raw_data <- read.csv("Output/FluxN2O.csv", stringsAsFactors = FALSE) 
test_data <- raw_data %>% mutate(Chamber = as.factor(Chamber), Treatment = as.factor(Treatment. Time = as.POSIXct(Time, format = "%Y-%m-%d %H:%M:%S"))) 

这里是head()和`STR():

> head(test_data) 
      Time Chamber_closed   Slope R_Squared Chamber Treatment Flux_N2O Time_relative Time_cumulative 
1 2016-05-03 00:08:21   10.23 8.873843e-07 0.6941540  10  AN 0.7567335   0.0    0.0 
2 2016-05-03 06:10:21   12.24 -5.540907e-06 0.7728001  12   U -4.7251117   362.0   362.0 
3 2016-05-03 06:42:21   10.24 -5.260463e-06 0.9583473  10  AN -4.4859581   32.0   394.0 
4 2016-05-03 07:12:21   9.23 -5.320429e-06 0.7602987  9  IU -4.5370951   30.0   424.0 
5 2016-05-03 07:42:21   7.23 3.135043e-06 0.7012436  7   U 2.6734669   30.0   454.0 
6 2016-05-03 20:10:15   5.24 5.215290e-06 0.7508935  5  AN 4.4474364   747.9   1201.9 


> str(Flux_output) 
'data.frame': 2234 obs. of 7 variables: 
$ Time   : POSIXct, format: "2016-04-21 15:34:22" "2016-04-21 15:42:36" ... 
$ Chamber_closed: num 16.1 15.1 16.2 15.2 14.1 12.1 13.1 10.1 9.1 7.1 ... 
$ Slope   : num -0.000246 0.000162 0.00279 -0.002263 0.002563 ... 
$ R_Squared  : num 0.575 0 0.302 0.462 0.299 ... 
$ Chamber  : Factor w/ 13 levels "1","3","4","5",..: 13 12 13 12 11 9 10 8 7 6 ... 
$ Treatment  : Factor w/ 4 levels "AN","IU","std_amb",..: 2 1 2 1 4 4 3 1 2 4 ... 
$ Flux_N2O  : num -210 138 2379 -1929 2186 ... 

然后我跑了以下代码来生成包含$ Total_emmissions矩阵的cum_ems_totals数据帧:

cum_ems <- Flux_output %>% 
    filter(Chamber != "13", R_Squared > 0.6, Time > "2016-05-03 00:00:00") %>% 
    group_by(Chamber) %>% 
    mutate(Time_relative = difftime(Time, lag(Time, default = Time[1]), units = c("hours")), 
     Time_relative = as.numeric(Time_relative), 
     Time_cumulative = cumsum(Time_relative), 
     cumulative_emissions=cumtrapz(Time_cumulative, Flux_N2O)) 

cum_ems_totals <- cum_ems %>% group_by(Chamber) %>% 
    summarise(Total_emmissions = last(cumulative_emissions)) %>% 
    mutate(Treatment = revalue(Chamber, c("1" = "U", "3" = "IU","4" = "AN","5" = "AN","6" = "IU","7" = "U", "9" = "IU","10" = "AN", "12" = "U","14" = "U", "15" = "AN", "16" = "IU")), 
     Block = revalue(Chamber, c("1"="1", "3"="1", "4"="1", "5"="2", "6"="2", "7"="2", "9"="3", "10" = "3", "12"="3", "14" = "4", "15" = "4", "16" = "4"))) 

> str(cum_ems_totals) 
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 12 obs. of 4 variables: 
$ Chamber   : Factor w/ 13 levels "1","3","4","5",..: 1 2 3 4 5 6 7 8 9 11 ... 
$ Total_emmissions: num [1:101, 1] 5769 7790 5167 7626 1964 ... 
$ Treatment  : Factor w/ 4 levels "U","IU","AN",..: 1 2 3 3 2 1 2 3 1 1 ... 
$ Block   : Factor w/ 5 levels "1","2","3","13",..: 1 1 1 2 2 2 3 3 3 5 ... 

那么,谁能告诉我为什么矩阵出现insted的标准向量,以及如何我可以相应地更改代码。 我推测它已经形成这行代码

summarise(Total_emmissions = last(cumulative_emissions)) 

谢谢!

+1

它看起来像'cumtrapz'返回一列矩阵。要解决您的问题,可能需要执行诸如c(cumtrapz(Time_cumulative,Flux_N2O)))''。 – aosmith

+0

@Sathish我添加了'str(Flux_output)'。 'dput'太大了,无法在这里添加 –

+0

@aosmith工作得很好 - 如果你把它写成答案我会接受它。谢谢 –

回答

3

cumtrapz函数返回1列矩阵。将它转换为矢量将避免该问题,可以通过c完成。

cumulative_emissions = c(cumtrapz(Time_cumulative, Flux_N2O)) 

顺便说一句,如果有未分组tibble工作,试图在矩阵使用last返回一个错误:

Error: Each variable must be a 1d atomic vector or list. Problem variables: 'cumulative_emissions'