Deleting multiple columns in R

前端 未结 4 1478
青春惊慌失措
青春惊慌失措 2021-01-27 11:29

I have a dataframe df with column names from m1 to m100

I want to delete columns in the range m50 to m100. Is there a faster way to do it than hardcoding it

4条回答
  •  渐次进展
    2021-01-27 12:02

    We can assign the columns to NULL in data.table

    library(data.table)
    setDT(df_cohort)[, paste0('M', 50:100) := NULL]
    

    If we need to subset,

    setDT(df_cohort)[, setdiff(names(df_cohort), 
                        paste0('m', 50:100)), with=FALSE]
    

提交回复
热议问题