Hi, I have data like this.... print(x) ID VAL1 VAL2 1 B A 2 P Q 3 T S What I would like is data like this... ID VAL1 VAL2 1 A B 2 P Q 3 S T So that VAL1 and VAL2 are alphabetically ordered. Any advice is welcome! San. [[alternative HTML version deleted]]
Try this: x[2:3] <- t(apply(x[-1], 1, sort)) On Thu, Feb 3, 2011 at 1:57 PM, sudhir cr <sudhir.cr@gmail.com> wrote:> Hi, > > > > I have data like this.... > > > > print(x) > > > > ID VAL1 VAL2 > > 1 B A > > 2 P Q > > 3 T S > > > > What I would like is data like this... > > > > ID VAL1 VAL2 > > 1 A B > > 2 P Q > > 3 S T > > > > > > So that VAL1 and VAL2 are alphabetically ordered. > > > > Any advice is welcome! > > > > San. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Hi:
u <- matrix(sample(LETTERS[1:24]), nrow = 8)
u
[,1] [,2] [,3]
[1,] "W" "J" "M"
[2,] "K" "V" "P"
[3,] "X" "E" "U"
[4,] "T" "B" "N"
[5,] "I" "Q" "F"
[6,] "G" "R" "S"
[7,] "A" "C" "O"
[8,] "D" "H" "L"
t(apply(u, 1, function(x) sort(x)))
[,1] [,2] [,3]
[1,] "J" "M" "W"
[2,] "K" "P" "V"
[3,] "E" "U" "X"
[4,] "B" "N" "T"
[5,] "F" "I" "Q"
[6,] "G" "R" "S"
[7,] "A" "C" "O"
[8,] "D" "H" "L"
HTH,
Dennis
On Thu, Feb 3, 2011 at 7:57 AM, sudhir cr <sudhir.cr@gmail.com> wrote:
> Hi,
>
>
>
> I have data like this....
>
>
>
> print(x)
>
>
>
> ID VAL1 VAL2
>
> 1 B A
>
> 2 P Q
>
> 3 T S
>
>
>
> What I would like is data like this...
>
>
>
> ID VAL1 VAL2
>
> 1 A B
>
> 2 P Q
>
> 3 S T
>
>
>
>
>
> So that VAL1 and VAL2 are alphabetically ordered.
>
>
>
> Any advice is welcome!
>
>
>
> San.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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.
>
[[alternative HTML version deleted]]