我是R新手,正在try 学习如何创建自己的函数.
虽然以下功能工作正常:
#---------------------
# this works fine
#---------------------
func <- function(df) {
new_df <- unite(df, key, c("Sepal.Length","Sepal.Width"), sep = " ", remove = FALSE, na.rm = FALSE)
return(new_df)
}
new_iris <- func(iris)
,该函数中unite函数的第三个参数现在已参数化:
#---------------------
# this does not work
#---------------------
func <- function(df, keycols) {
new_df <- unite(df, key, keycols, sep = " ", remove = FALSE, na.rm = FALSE)
return(new_df)
}
keycols <- quote(c("Sepal.Length","Sepal.Width"))
new_iris <- func(iris, keycols)
生成以下错误消息:
Error: Must subset columns with a valid subscript vector. x Subscript has the wrong type 100. i It must be numeric or character.
有没有办法将c("萼片长度"、"萼片宽度")作为参数传递?或者通过某种方式使keycols成为上述用户定义函数的参数?
谢谢你的指导.