简体   繁体   中英

Create a variable to invoke a data.frame in R

So I have created a program that runs a summary and anova as well as plots some graphs for me. The problem is that for each new data frame I use I need to change the variables inside the formulas. What I want to do is create a variable at the beginning of the script that I assign to the column I'm interested in and then the program does the work:

mydata <- Leaves.data.csv
attach(mydata)
str(mydata)

var <- Leaves

avgVaL <- group_by(mydata, Treatment, Medium, Treatment:Medium) %>% 
summarise(count=sum(!is.na(var)), mean = mean(var, na.rm = T), sd = sd(var, na.rm=T), se = sd/sqrt(count)) 

The only thing I wish to change is Leaves . The problem with this code is summarise takes var as 1 single variable and returns the count, mean, sd and se of the all the data points instead of each group.

In the end I needed to use the quo() function as this function quotes my input rather than evaluation, thus (if I understand correctly) quoting would be similar to calling in other programming languages, which means you invoke directly that variable from the original data frame rather than creating a new one altogether. At the same time you have to use !! behind every call inside the function of interest as this tells the function to evaluate the already quoted variable (rather than quoting again).

Much better explained: https://cran.r-project.org/web/packages/dplyr/vignettes/programming.html

The code:

var <- quo(Root.growth)
avgVar <- group_by(mydata, Treatment, Medium) 
%>% summarise(count=sum(!is.na(!!var)), mean = mean(!!var, na.rm = T), sd = sd(!!var, na.rm=T), se = sd/sqrt(count)) 

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM