[英]A list contain column names and using the list to reference the tibble in R
The table contain My Name, My Age , My Height.该表包含我的名字、我的年龄、我的身高。 I would like to store in a list to get the summary of the height instead of using test = My Height
我想存储在列表中以获取高度的摘要,而不是使用test = My Height
list<- c("`My Name`" , "`My Age`" , "`My Height`" )
table%>%
group_by(`My Name` ,`My Age`,`My Height` ) %>%
summarize(test = mean(list[3], na.rm = TRUE))
Try summarize_at
:尝试
summarize_at
:
library(dplyr)
lst<- c("`My Name`" , "`My Age`" , "`My Height`" )
table%>%
group_by(`My Name` ,`My Age`,`My Height` ) %>%
summarize_at(vars(lst[3]), mean, na.rm = TRUE)
Or using non-standard evaluation :或使用非标准评估:
table %>%
group_by(`My Name` ,`My Age`,`My Height` ) %>%
summarise(test = mean(!!sym(lst[2]), na.rm = TRUE))
Using reproducble example form mtcars
使用可重现的示例形式
mtcars
lst <- c('cyl', 'hp', 'am')
mtcars %>%
group_by(cyl) %>%
summarise_at(vars(list[2]), mean, na.rm =TRUE)
# A tibble: 3 x 2
# cyl test
# <dbl> <dbl>
#1 4 82.6
#2 6 122.
#3 8 209.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.