r - 根据列名将多列更改为 as.numeric
问题描述
我正在寻找与此解决方案等效的解决方案:
df[cols] <- lapply(df[cols], as.numeric)
df[4:10]
但我希望使用列名而不是列索引df[c(name1, name2, name3)]
非常感谢任何和所有帮助!N。
解决方案
columns_vector <- c("Col1", "Col2")
for (col in columns_vector) {
df1[col] <- as.numeric(df1[col])
}