Reordering columns in a large dataframe

前端 未结 8 720
感动是毒
感动是毒 2020-12-12 21:25

Using the following example dataframe:

a <-  c(1:5)
b <- c(\"Cat\", \"Dog\", \"Rabbit\", \"Cat\", \"Dog\")
c <- c(\"Dog\", \"Rabbit\", \"Cat\", \"Do         


        
相关标签:
8条回答
  • 2020-12-12 22:23

    The package dplyr and the function dplyr::relocate, a new verb introduced in dplyr 1.0.0, does exactly what you are looking for.

    df %>% dplyr::relocate(b, c, .after = f)

    0 讨论(0)
  • 2020-12-12 22:25

    Use the subset function:

    > df <- data.frame(a,b,c,d,e,f)
    > df <- subset(df, select = c(a, d:f, b:c))
    > df
      a      d      e      f      b      c
    1 1 Rabbit    Cat    Cat    Cat    Dog
    2 2    Cat    Dog    Dog    Dog Rabbit
    3 3    Dog    Dog    Dog Rabbit    Cat
    4 4    Dog Rabbit Rabbit    Cat    Dog
    5 5 Rabbit    Cat    Cat    Dog    Dog
    
    0 讨论(0)
提交回复
热议问题