Deleting multiple columns in R

前端 未结 4 1475
青春惊慌失措
青春惊慌失措 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 11:56

    With dplyr you could do it like this:

    library(dplyr)
    df <- select(df, -(M50:M100))
    

    This removes all columns between column "M50" and column "M100".

    A different option, that does not depend on the order of columns is to use

    df <- select(df, -num_range("M", 50:100))
    

提交回复
热议问题