【发布时间】:2019-03-28 19:11:54
【问题描述】:
我正在尝试在 R 中编写一个函数:
1) 接收数据框和列名作为参数。 2) 对数据框中的列执行操作。
func <- function(col, df)
{
col = deparse(substitute(col))
print(paste("Levels: ", levels(df[[col]])))
}
func(Col1, DF)
func(Col2, DF)
mapply(func, colnames(DF)[1:2], DF)
输出
> func(Col1, DF)
[1] "Levels: GREEN" "Levels: YELLOW"
> func(Col2, DF)
[1] "Levels: 0.1" "Levels: 1"
> mapply(func, colnames(DF)[1:2], DF)
Error in `[[.default`(df, col) : subscript out of bounds
【问题讨论】:
-
您能否将您的
DF显示为一个可重复的小示例
标签: r dataframe apply sapply mapply