Move a column to first position in a data frame

前端 未结 8 1524
别那么骄傲
别那么骄傲 2020-12-22 23:30

I would like to have the last column of the data frame moved to the start (as first column). How can I do it in R?

My data.frame has about a thousand columns to chan

8条回答
  •  死守一世寂寞
    2020-12-22 23:54

    You can change the order of columns by adressing them in the new order by choosing them explicitly with data[,c(ORDER YOU WANT THEM TO BE IN)]

    If you just want the last column to be first use: data[,c(ncol(data),1:(ncol(data)-1))]

    > head(cars)
      speed dist
    1     4    2
    2     4   10
    3     7    4
    4     7   22
    5     8   16
    6     9   10
    
    > head(cars[,c(2,1)])
      dist speed
    1    2     4
    2   10     4
    3    4     7
    4   22     7
    5   16     8
    6   10     9
    

提交回复
热议问题