0

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

  1. Would you please solve this error?
  2. 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.

Dave2e
  • 22,192
  • 18
  • 42
  • 50
Harry
  • 45
  • 4
  • 2
    Use `{{colname}}` in the function. If you pass column name as string `mean_byname("value2")` you can use https://stackoverflow.com/questions/48062213/dplyr-using-column-names-as-function-arguments – Ronak Shah Mar 27 '21 at 14:22
  • 1
    @RonakShah's answer is correct. The *reason* you get the error is that tidyverse uses non-standard evaluation [NSE](https://dplyr.tidyverse.org/articles/programming.html). – Limey Mar 27 '21 at 18:42
  • 1
    Does this answer your question? [dplyr - using column names as function arguments](https://stackoverflow.com/questions/48062213/dplyr-using-column-names-as-function-arguments) – Sinh Nguyen Mar 27 '21 at 22:39

0 Answers0