0

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!

  • Don't use `$` with variables. Use `[[`. For example `df[[x]]` or if you are building formulas, use `reformulate()` – MrFlick Jan 15 '18 at 20:22
  • Thanks alot for your quick reply! I have now tried mean(df[,get(x)]) (cp. edited question above) but that throws an error, too? – Luitpold Wienerle Jan 15 '18 at 20:37
  • Use `mean(df[[x]]])` or `mean(df[, x])`. Leave `get()` out of it. – MrFlick Jan 15 '18 at 20:39
  • Thank you so much, MrFlick, that is indeed an simple solution! I still think my question was no dublicate as the older question you are referencing to imho seems more complex or less straight forward to me. Would you mind reopening my questions and answering something short like "The solution is for(x in names(data)){ print(mean(df[,x])) } ? – Luitpold Wienerle Jan 15 '18 at 20:51
  • @LuitpoldWienerle - The answer referenced in the duplicate notice does correctly answer this exact question. – Len Greski Jan 15 '18 at 21:47

0 Answers0