I can successfully use get() to iterate over variable names when a function allows to specify the dataframe seperately (like e.g. aggregate), but not when the function expects a df$var notation (like e.g. mean). I tried paste and envir but have not yet been able to solve the problem.
df = mtcars
#the following works as I can specify the list of variables as well as the dataframe
for(x in names(data)){
print(aggregate(get(x) ~ carb, data=df, mean))
}
#but mean instead of aggregate while trying to set the dataframe via envir throws an error:
for(x in names(data)){
print(mean(get(x), envir = df))
}
#Error in get(x) : object 'mpg' not found
#and pasting df$ and x results in another error:
for(x in names(data)){
print(mean(get(paste0("df$",x))))
}
#Error in get(paste0("df$", x)) : object 'df$mpg' not found
#I guess because
exists("df$mpg")
#is FALSE
#but how can I make the object exist and why does the following work?!
mean(df$mpg)
Also a [] notation throws an error
for(x in names(data)){
print(mean(df[,get(x)]))
}
#Error in get(x) : object 'mpg' not found
#while
mean(df[,"mpg"])
#works
I guess there should be a simple solution but despite of a lot of searching I have not found one. Many thanks in advance!