Sort columns of a dataframe by column name

后端 未结 9 643
自闭症患者
自闭症患者 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:26

    An alternative option is to use str_sort() from library stringr, with the argument numeric = TRUE. This will correctly order column that include numbers not just alphabetically:

    str_sort(c("V3", "V1", "V10"), numeric = TRUE)

    # [1] V1 V3 V11

提交回复
热议问题