如何使用“starts_with”函数对数据帧进行子集化?

时间:2021-07-15 18:01:36

标签: r

如何将“starts_with”嵌入到我的字段列表中的数据框子集?这是我到目前为止的尝试。该错误要求将其与选择功能一起使用,尽管我认为我正在这样做?谢谢!

df <- subset(raw_df, select=c(col1, col2, col3, V1, V2, starts_with("V")))

1 个答案:

答案 0 :(得分:1)

starts_with 是来自 tidyselect 包的选择助手。我们可能需要来自 startsWith

base R
subset(raw_df, select=c(col1, col2, col3, 
         names(raw_df)[startsWith(names(raw_df), "V")]))

这在 dplyr 中也可能更紧凑

library(dplyr)
raw_df %>%
    select(matches('^col[1-3]$'), starts_with("V"))