Rory Shaw
Rory Shaw

Reputation: 851

Matrix appearing inside a dataframe - how has this happened?

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")))

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

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

Thanks!

Upvotes: 1

Views: 107

Answers (1)

aosmith
aosmith

Reputation: 36076

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:

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

Upvotes: 3

Related Questions