I have the following excel dataset sample1.xlsx and wishes to calculate the mean for each group:
name, value1, value2
C1 5 0.5
C1 6 0.4
C1 7 0.8
T1 10 2.2
T1 20 4.6
T1 30 2.9
T2 100 10.1
T2 200 30.5
T2 300 20.5
library(tidyverse)
library(readxl)
xfile <- read_excel("sample1.xlsx")
xfile %>%
group_by(name) %>%
summarize(mean(value1))
# This works
However, when turning it into a function (so that mean of value2 can be calculated as well):
mean_byname <- function(colname){
xfile %>%
group_by(name) %>%
summarize(mean(colname))
}
mean_byname(value2)
# returns error msg Error: Problem with `summarise()` input `..1`.
- Would you please solve this error?
- If I want to obtain a vector which includes unique value in "name", ie C1 T1 T2 in this case, is there a simpler alternative than this?:
xname <- xfile %>%
group_by(name) %>%
summarize(mean(value1))
xname$name
Thank you very much.