-1
install.packages(c("ggplot2", "ggpubr", "tidyverse", "broom", "AICcmodavg"))
library(ggplot2)
library(ggpubr)
library(tidyverse)
library(broom)
library(AICcmodavg)
               
my_data <- read.table(file = "clipboard", 
                      sep = "\t", header=TRUE)

group_by(my_data, group) %>%
mutate(across(co2, as.numeric)) %>% 
suppressWarnings(as.numeric(co2))
summarise (co2, count = n(), mean = mean("CO2", na.rm = TRUE), sd = sd("CO2", na.rm = TRUE))

Working my way towards carrying out a one way anova test with a dataset and I keep getting this same error code:

No applicable method for 'summarise' applied to an object of class "ts"

When I run the following line of code:

summarise (co2, count = n(), mean = mean("CO2", na.rm = TRUE), sd = sd("CO2", na.rm = TRUE))

My dataset involves no timeseries (a table of CO2 emissions compared to vegetation type) so I'm not sure why I'm getting this error and what I can do to solve it? Any help would be much appreciated, thank you!

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
  • Isn't this similar to your earlier question [here](https://stackoverflow.com/questions/71010647/r-studio-error-n-must-only-be-used-inside-dplyr-verbs) – akrun Feb 06 '22 at 20:40
  • Where does your `co2` Object come from? What have you tried in the 45 minutes or so since your last question? It’s unlikely that a string as a first argument to a summary function (eg `mean`) is correct. It’s bad to use the name of a function (eg `mean`) as an object or column name. – Limey Feb 06 '22 at 20:51
  • My CO2 object comes from a separate excel sheet. My only real change is the addition of this line- mutate(across(co2, as.numeric)) %>% - which solved the previous error but created this new one immediately! So far I'm just working through applying the suggestions made in the recommended other post (summarise monthly seasonal factors) to see if that helps. What would you recommend changing the first argument to? Thanks for the help! – Jack Neath Feb 06 '22 at 21:08
  • This seems like a typo—did you mean to have a pipe going into your `summarise` call? It's hard to know for sure without a [reproducible example](https://stackoverflow.com/q/5963269/5325862). The `suppressWarnings` line doesn't make sense either, since you probably meant for that to be inside the `mutate` call – camille Feb 07 '22 at 01:00

1 Answers1

1

There are (at least) three problems with your code:

  1. This code is meaningless:

    ... %>% 
      suppressWarnings(as.numeric(co2))
    

    This call to suppressWarnings really evaluates to

    ... %>% 
      suppressWarnings(., as.numeric(co2))
    

    which argument-expands to

    ... %>% 
      suppressWarnings(expr = ., classes = as.numeric(co2))
    

    which ... does absolutely nothing. Fortunately, it passes the frame passed to it with no change, so it will not error ... but it also does absolutely nothing to the data, including what you may have originally intended with as.numeric(.). If you want to convert co2 to numeric from something else, this should likely be

    ... %>% 
      mutate(co2 = suppressWarnings(as.numeric(co2)))
    

    However ... that seems completely redundant given that the line before that is mutate(across(co2, as.numeric)), which does precisely that.

  2. Because you do not continue the pipeline from the suppressWarnings(.) line, your next call is

    summarise (co2, ...)
    

    If you had differently-named variables, this should fail with object 'somethingelse' not found, but you used co2. That happens to be the name of co2 in the datasets package, which is not a data.frame, it is class ts. Type in ?co2 to see what that data is about, and just co2 to see what you were really passing there.

    Fix this problem by adding %>% to the previous line. Actually, removing the previous line (and using the fact that the previous previous line does have the %>%), it may be something like:

    group_by(my_data, group) %>%
      mutate(across(co2, as.numeric)) %>% 
      summarise(...)
    

    For efficiency, though, doing that mutate within a grouping is inefficient. I suggest instead:

    my_data %>%
      mutate(across(co2, ~ suppressWarnings(as.numeric(.)))) %>%
      group_by(group) %>%
      summarise(...)
    
  3. Your summarise call is confuses me.

    summarise(
      co2,
      count = n(),
      mean = mean("CO2", na.rm = TRUE),
      sd = sd("CO2", na.rm = TRUE)
    )
    
    • co2, assuming it is really a column in the data, will "summarise" to the same number of rows with no aggregation of co2. Perhaps this should be some aggregating function, like min or max? I'll remove this from my suggested code below.
    • count = n() makes sense, but it returns a single number, so when combined with co2 above, it will repeat this one number nrow(.) times. When we remove co2 above, this will be fine.
    • mean = mean("CO2", na.rm=TRUE) ... what is the average of a string? Do you mean mean = mean(co2, na.rm=TRUE)?
    • sd = sd("CO2", na.rm=TRUE) ... ditto.

Ultimately, try this:

my_data %>%
  mutate(across(co2, ~ suppressWarnings(as.numeric(.)))) %>%
  group_by(group) %>%
  summarise(
    count = n(),
    mean = mean(co2, na.rm = TRUE),
    sd = sd(co2, na.rm = TRUE)
  )
r2evans
  • 141,215
  • 6
  • 77
  • 149