我最近针对使用dplyr
函数group_by
和summarise
突然出现的错误发布了question。
出错的原因是由于在数据帧中有一个列矩阵而不是常规向量。
这是通过将矩阵强制转换为矢量来解决的...但是,我现在想知道的是它是如何实现的!
我正在处理的数据集可以是downloaded here,并使用以下代码准备好使用:
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 ...
然后我运行以下代码来生成包含cum_ems_totals
矩阵的$ Total_emmissions
数据框:
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 ...
所以任何人都可以告诉我为什么矩阵出现了标准向量,以及我如何相应地更改代码。 我认为它来自这行代码
summarise(Total_emmissions = last(cumulative_emissions))
谢谢!
答案 0 :(得分:3)
cumtrapz
函数返回1列矩阵。将其转换为向量将避免此问题,可以通过c
完成。
cumulative_emissions = c(cumtrapz(Time_cumulative, Flux_N2O))
顺便说一句,如果使用未组合的tibble,尝试在矩阵上使用last
会返回错误:
错误:每个变量必须是1d原子向量或列表。问题 变量:&#39; cumulative_emissions&#39;