[R] rearrange problem

jim holtman jholtman at gmail.com
Thu Sep 13 12:50:42 CEST 2007


Is this what you want:

> n <- 10
> m <- 4
> x <- replicate(n, paste(letters[sample(1:26,m)], collapse=''))
> x
 [1] "zmld" "tlme" "fonb" "aqwn" "onxl" "rpfx" "sler" "cvom" "ilme" "nbge"
> x.sort <- sapply(x, function(.str){
+     paste(sort(strsplit(.str, '')[[1]]), collapse='')
+ })
> x.sort
  zmld   tlme   fonb   aqwn   onxl   rpfx   sler   cvom   ilme   nbge
"dlmz" "elmt" "bfno" "anqw" "lnox" "fprx" "elrs" "cmov" "eilm" "begn"
>


On 9/13/07, kevinchang <shukai at seas.upenn.edu> wrote:
>
> Hi All,
>
> I am trying to rearrange alphabetically each element in a character vector.
> ex: say the first element in the vector is "cba", and my goal is to turn it
> into "abc". The suggested function to use is sort(). But it turns out that
> sort doesn't work at the level of element. So I am wondering that if there
> is an alternative function for this purpose. Please help me out . Thanks.
> --
> View this message in context: http://www.nabble.com/rearrange-problem-tf4434906.html#a12652394
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list