Sort columns of a dataframe by column name

后端 未结 9 625
自闭症患者
自闭症患者 2020-11-27 10:48

This is possibly a simple question, but I do not know how to order columns alphabetically.

test = data.frame(C = c(0, 2, 4, 7, 8), A = c(4, 2, 4, 7, 8), B =          


        
9条回答
  •  半阙折子戏
    2020-11-27 11:27

    test = data.frame(C=c(0,2,4, 7, 8), A=c(4,2,4, 7, 8), B=c(1, 3, 8,3,2))
    

    Using the simple following function replacement can be performed (but only if data frame does not have many columns):

    test <- test[, c("A", "B", "C")]
    

    for others:

    test <- test[, c("B", "A", "C")]
    

提交回复
热议问题