使用列表和名称在 R 中选择列

Selecting Columns in R using list and names

我发现这个问题有助于仅为数字列子集 DF。

Selecting only numeric columns from a data frame

但是我不知道如何处理数字列和任何其他列。

我试过:

nums <- sapply(df, is.numeric)
df <- df[, c(nums, "charcolumn")]

和:

df <- df[,c(sapply(df, is.numeric), "Pop_Size_Group")]

两者都给了我一个 "undefined columns selected error"

我知道 sapply 函数为我提供了 TRUE/FALSE 的列表。我如何子集我的 df 以包括所有数字列加上我识别的其他列?

也许选择 names 并将它们与 "Pop_Size_Group"

连接起来
 df <- df[,c(names(df)[sapply(df, is.numeric)], "Pop_Size_Group")]