Sorting the values of column in ascending order in R

别来无恙 提交于 2019-12-02 08:20:44

For the sake of completeness, here is an answer using data.table:

library(data.table)
cols <- c("a3", "a4")
setDT(a123)[, (cols) := lapply(.SD, sort), by = .(a1, a2), .SDcols = cols][]
   a1 a2 a3  a4
1:  A  D  5  65
2:  B  E 15 130
3:  C  F 12  49
4:  A  D 10  80
5:  B  E 40 160
6:  C  E 35 150
7:  A  D 20 100
8:  C  F 50  66

Data

a1 = c("A","B","C","A","B","C","A","C")
a2 = c("D","E","F","D","E","E","D","F")
a3 = c(5,15,12,10,40,35,20,50)
a4 = c(100,160,66,65,130,150,80,49)
a123= data.frame(a1,a2,a3,a4)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!