I have calculated the mean of the observations for each treatment using
Data %>% group_by(Treatment, Rep) %>% summarise(Mean = mean(Nitrogen, na.rm = TRUE))
I have got a single value for each treatment. Now I want to calculate the standard deviation and standard error of the mean. For which I have used,
Data %>% group_by(Treatment, Rep) %>% summarise_each(funs = mean, sd, se=sd(.)/sqrt(n()), na.rm = TRUE)
But it gives an error. I am not sure what is my mistake. Thank you!
summarise_each
is getting deprecated and funs
is replaced by list
library(dplyr)
Data %>%
group_by(Treatment) %>%
summarise_at(vars(-group_cols()), list(mean = ~mean(., na.rm = TRUE),
sd = ~sd(., na.rm = TRUE),
se=~ sd(., na.rm = TRUE)/sqrt(n())))
If we are not sure about the column types, check
str(Data)
and apply the functions only on the numeric columns. Without changing much in the previous code, replace the summarise_at
to summarise_if
for numeric columns
Data %>%
group_by(Treatment, Rep) %>%
summarise_if(is.numeric, list(mean = ~mean(., na.rm = TRUE),
sd = ~sd(., na.rm = TRUE),
se=~ sd(., na.rm = TRUE)/sqrt(n())))
If some columns have class
factor
and needs to be used for the mean/sd
, then first convert those column/columns to numeric
with as.numeric(as.character(Data[[yourcolumn]]))
It can be reproduced with iris
data
data(iris)
iris %>%
group_by(Species) %>%
summarise_at(vars(-group_cols()), list(mean = ~mean(., na.rm = TRUE),
sd = ~sd(., na.rm = TRUE),
se=~ sd(., na.rm = TRUE)/sqrt(n())))
# A tibble: 3 x 13
# Species Sepal.Length_me… Sepal.Width_mean Petal.Length_me… Petal.Width_mean Sepal.Length_sd Sepal.Width_sd Petal.Length_sd
# <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 setosa 5.01 3.43 1.46 0.246 0.352 0.379 0.174
#2 versic… 5.94 2.77 4.26 1.33 0.516 0.314 0.470
#3 virgin… 6.59 2.97 5.55 2.03 0.636 0.322 0.552
# … with 5 more variables: Petal.Width_sd <dbl>, Sepal.Length_se <dbl>, Sepal.Width_se <dbl>, Petal.Length_se <dbl>,
# Petal.Width_se <dbl>
In the OP's post, some of the functions have anonymous function and na.rm = TRUE
seems to be from the mean
(not clear).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.