I want to build a function that takes a variable name as an argument so if I have some code like:
function(df, variable = NULL) {
df <- df %>% filter(variable > 10)
test <- c()
for (i in c(1:10)) {
test2 <- df$variable[i] - df$othervariable[i]
test <- c(test, test2)
}
return(test)
}
Then I can swap out the variable simply by plugging in the name through the argument. I've thought about doing it like df['variable']
instead. But this does not solve the problem in the dplyr filter function and also it returns a dataframe rather than df$variable
which is numeric.
df$variable
与df[,'variable']
(相同,
很重要)相同,也与df[['variable']]
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.