Reduce dataframe to single row by mean

Hi there!

I have the following dataset that I am trying to reduce to a single row by taking the mean of all the depths. When I try to use summarise() it gives me an error:

Error: Problem with `summarise()` input `..2`.
x Input `..2` must be a vector, not a function.
i Input `..2` is `mean`.

What I want it to look like is a dataframe with a single row and two columns: the SITE_ID and the MEAN_DEPTH_M.

Here's the dataset:

secchi <- structure(list(SITE_ID = c("GBA20-10501", "GBA20-10501", "GBA20-10501", 
"GBA20-10501", "GBA20-10501", "GBA20-10501"), DEPTH_M = c("2.3", 
"2.0", "2.2", "2.0", "2.2", "2.0")), row.names = 4:9, class = "data.frame")

I tried this:

secchi %>% summarise(DEPTH_M, mean, na.rm=T)

Can anyone help me figure this out?

Thanks so much!

One problem is that the DEPTH_M column in the data frame is entered as characters, so I put in a line of code to convert that column to numbers. I used the group_by() function so that the SITE_ID appears in the final data frame. Finally, the syntax of summarise() is not correct in your example.

secchi <- structure(list(SITE_ID = c("GBA20-10501", "GBA20-10501", "GBA20-10501", 
                                      "GBA20-10501", "GBA20-10501", "GBA20-10501"), 
                          DEPTH_M = c("2.3","2.0", "2.2", "2.0", "2.2", "2.0")), 
                     row.names = 4:9, class = "data.frame")
secchi %>% mutate(DEPTH_M = as.numeric(DEPTH_M)) %>% 
   group_by(SITE_ID) %>% 
   summarise(Avg = mean(DEPTH_M, na.rm = TRUE))
`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 1 x 2
  SITE_ID       Avg
  <chr>       <dbl>
1 GBA20-10501  2.12
1 Like
secchi <- structure(list(SITE_ID = c(
    "GBA20-10501", "GBA20-10501", "GBA20-10501",
  "GBA20-10501", "GBA20-10501", "GBA20-10501"
), DEPTH_M = c(
  "2.0", "2.2", "2.0", "2.2", "2.0"
)), row.names = 4:9, class = "data.frame")

secchi[2] <- as.numeric(secchi[[2]])
secchi <- data.frame(SITE_ID = "GBA20-10501",
                     DEPTH_M = colMeans(secchi[2]))
#>             SITE_ID  DEPTH_M
#> DEPTH_M GBA20-10501 2.116667

Ahhhh thank you for the help with the syntax and also for reminding me to check if my data is character or numeric! This worked beautifully, thanks a ton for your help!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.