Deleting multiple columns in R

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

    Assuming you have something like:

    mydf <- data.frame(matrix(1:100, ncol = 100, 
                              dimnames = list(NULL, paste0("m", 1:100))))
    

    Simply do:

    mydf[paste0("m", 50:100)] <- list(NULL)  ## This is pretty destructive ;-)
    

    By the way, you can also do:

    subset(mydf, select = m1:m49)
    

    or

    subset(mydf, select = -(m50:m100))
    

提交回复
热议问题