矩阵出现在数据框内 - 这是怎么发生的? [英] Matrix appearing inside a dataframe - how has this happened?

查看:194
本文介绍了矩阵出现在数据框内 - 这是怎么发生的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发布了一个问题关于使用 dplyr 函数 group_by 总结
错误的原因是由于在数据帧中有一列列,而不是常规向量。
这是通过将矩阵强制转换为一个向量来解决的,但是现在我想知道的是如何到达那里!

I recently posted a question regarding an error that cropped up using dplyrfunction group_by and summarise. The reason for the error was due to having a one column matrix within a dataframe instead of a regular vector. This was solved by coercing the matrix to a vector... however, what I want to know now is how it got there in the first place!

我正在处理的数据集可以是这里下载,并准备使用以下代码:

The dataset I am working on can be downloaded here and prepped for use using the following code:

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() / code>和`str():

Here is the head() and `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 dataframe,其中包含 $ Total_emmissions 矩阵:

I then ran the following code to produce the cum_ems_totals dataframe which contains the $ Total_emmissions matrix:

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 ...

所以有人可以告诉我为什么矩阵出现了一个标准的向量,我可以相应地更改代码。
我认为它已经来自这一行代码

So can anyone tell me why the matrix appears insted of a standard vector and how I can change the code accordingly. I presume it has come form this line of code

summarise(Total_emmissions = last(cumulative_emissions))

谢谢!

推荐答案

cumtrapz 函数返回1列矩阵。把它转换成一个向量而不是这个问题,这可以通过 c 来实现。

The cumtrapz function returns a 1 column matrix. Turning it in to a vector instead will avoid the problem, which can be done via c.

cumulative_emissions = c(cumtrapz(Time_cumulative, Flux_N2O))

如果使用未分组的字符串,尝试在矩阵上使用最后一个返回错误:

As an aside, if working with an ungrouped tibble, trying to use last on a matrix returns an error:


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

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

这篇关于矩阵出现在数据框内 - 这是怎么发生的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆