Drop data frame columns by name

后端 未结 20 2797
花落未央
花落未央 2020-11-22 01:06

I have a number of columns that I would like to remove from a data frame. I know that we can delete them individually using something like:

df$x <- NULL
<         


        
20条回答
  •  一整个雨季
    2020-11-22 01:50

    DF <- data.frame(
      x=1:10,
      y=10:1,
      z=rep(5,10),
      a=11:20
    )
    DF
    

    Output:

        x  y z  a
    1   1 10 5 11
    2   2  9 5 12
    3   3  8 5 13
    4   4  7 5 14
    5   5  6 5 15
    6   6  5 5 16
    7   7  4 5 17
    8   8  3 5 18
    9   9  2 5 19
    10 10  1 5 20
    

    DF[c("a","x")] <- list(NULL)
    

    Output:

            y z
        1  10 5
        2   9 5
        3   8 5
        4   7 5
        5   6 5
        6   5 5
        7   4 5
        8   3 5    
        9   2 5
        10  1 5
    

提交回复
热议问题