【问题标题】:Matrix appearing inside a dataframe - how has this happened?出现在数据框中的矩阵 - 这是怎么发生的?
【发布时间】:2016-12-03 01:49:35
【问题描述】:

我最近发布了一个question,关于使用dplyrfunction group_bysummarise 时出现的错误。 错误的原因是由于数据框中有一个单列矩阵而不是常规向量。 这是通过将矩阵强制转换为向量来解决的……但是,我现在想知道的是它最初是如何到达那里的!

我正在处理的数据集可以是 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 ...

然后我运行以下代码来生成包含$ 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 ...

那么谁能告诉我为什么矩阵出现在标准向量中,以及如何相应地更改代码。 我想它来自这行代码

summarise(Total_emmissions = last(cumulative_emissions))

谢谢!

【问题讨论】:

  • 看起来cumtrapz 返回一个单列矩阵。为了解决您的问题,您可以执行c(cumtrapz(Time_cumulative, Flux_N2O))) 之类的操作。
  • @Sathish 我已添加str(Flux_output)dput 太大,无法在此处添加
  • @aosmith 效果很好——如果你把它写成答案,我会接受。谢谢

标签: r matrix vector dataframe dplyr


【解决方案1】:

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

cumulative_emissions = c(cumtrapz(Time_cumulative, Flux_N2O))

顺便说一句,如果使用未分组的 tibble,尝试在矩阵上使用 last 会返回错误:

错误:每个变量必须是一维原子向量或列表。问题 变量:'cumulative_emissions'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多