Sorting rows alphabetically

ε祈祈猫儿з 提交于 2019-11-26 17:57:18
t(apply(DF, 1, sort))

The t() function is necessary because row operations with the apply family of functions returns the results in column-major order.

What did you try? This is really straight-forward and easy to solve with a simple loop.

> s <- x
> for(i in 1:NROW(x)) {
+   s[i,] <- sort(s[i,])
+ }
> s
  V1 V2 V3 V4
1  A  B  C  D
2  A  B  C  D
3  M  X  Y  Z
4  L  M  O  P

No plyr answer yet?!

foo <- matrix(sample(LETTERS,10^2,T),10,10)

library("plyr")

aaply(foo,1,sort)

Exactly the same as DWins answer except that you don't need t()

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!