Select the last n columns of data frame in R

前端 未结 7 1002
礼貌的吻别
礼貌的吻别 2021-01-04 04:42

Is there a way to systematically select the last columns of a data frame? I would like to be able to move the last columns to be the first columns, but maintain the order of

相关标签:
7条回答
  • 2021-01-04 05:03

    The problem described doesn't match the title, and existing answers address the moving columns part, doesn't really explain how to select last N columns.

    If you wanted to just select the last n columns in a matrix/data frame without knowing the column names:

    mydata2[,ncol(mydata2)]
    

    and if you want last n columns, try

    mydata[,(ncol(mydata2)-n-1):ncol(mydata2)]
    

    A little cumbersome, but works. Could write wrapper function if you plan to use it regularly.

    0 讨论(0)
提交回复
热议问题