1

Hello I have a table with 5 columns. One of the column X is:

x <- c(1,1,1,1,1,1,2,2,2,3)

How can I change the order of numbers in vector X, for example on the first place put 3s, on the second place put 1s and on the third place put 2s. The output should be in format like:

x <- c(3,1,1,1,1,1,1,2,2,2)

And replace not only the values in the column X but all other rows for each number of X

To clarify the question:

X(old version) -> X(new version)
     1                2
     2                3
     3                1
So, If X=1 make it X=2
    If X=2 make it X=3
    If X=3 make it X=1

And if for example we change X=1 to X=2 we should put all the rows for X=1 to X=2

I have two vectors:

 x <- c(1,1,1,1,1,1,2,2,2,3)
 z <- c(10,10,10,10,10,10,20,20,20,30)

The desired output:

x  z
1  30
2  10
2  10
2  10
2  10
2  10
2  10
3  20 
3  20
3  20
Arun
  • 108,644
  • 21
  • 263
  • 366
user45415631
  • 175
  • 1
  • 10
  • 2
    It is unclear what you're asking. Please provide a sample of the complete data set and desired result. – Rich Scriven Jul 19 '14 at 03:59
  • @RichardScriven Edited! – user45415631 Jul 19 '14 at 04:21
  • Possible duplicate of http://stackoverflow.com/questions/1296646/how-to-sort-a-dataframe-by-columns-in-r/1296745#1296745 – Rich Scriven Jul 19 '14 at 04:49
  • @RichardScriven It`s not a duplicate since I am asking not to change the order to ASC or DESC – user45415631 Jul 19 '14 at 16:17
  • @user45415631. Your updated version shows exactly the same idea that was before, but the expected result is different. X=1, make it X=2, X=2, make it X=3, if X=3, make it X=1. Using my code. x [1] 1 1 1 1 1 1 2 2 2 3; x1 [1] 2 2 2 2 2 2 3 3 3 1 just does what you asked. Also, z[order(x1)] # [1] 30 10 10 10 10 10 10 20 20 20, however `x` in desired output is not clear. – akrun Jul 19 '14 at 16:54
  • @user45415631. I just updated based on the `desired output`. – akrun Jul 19 '14 at 17:00

2 Answers2

1

You could

 x1 <-c(2,3,1)[x]
 x[order(x1)]
 # [1] 3 1 1 1 1 1 1 2 2 2

or

  x[order(chartr(old="123",new="231",x))]
  #[1] 3 1 1 1 1 1 1 2 2 2

Update

If you have many columns.

 x <- c(1,1,1,1,1,1,2,2,2,3)
 z <- c(10,10,10,10,10,10,20,20,20,30)
 set.seed(14)
 y <- matrix(sample(25,10*3,replace=TRUE),ncol=3)
 m1 <- as.data.frame(cbind(x,z,y))
 x1 <- c(2,3,1)[m1$x]
 x1
 # [1] 2 2 2 2 2 2 3 3 3 1


 res <- cbind(x=c(2,3,1)[m1$x[order(x1)]],subset(m1[order(x1),], select=-x))
 res
 #   x  z V3 V4 V5
 #10 1 30 10 15  2
 #1  2 10  7 23  9
 #2  2 10 16  5 11
 #3  2 10 24 12 16
 #4  2 10 14 22 18
 #5  2 10 25 22 19
 #6  2 10 13 19 16
 #7  3 20 24  9 10
 #8  3 20 11 17 14
 #9  3 20 13 22 18
akrun
  • 674,427
  • 24
  • 381
  • 486
  • Thank you! Do all other rows in table change the order along with X?. For example, if we change 3 to 1, I need all the rows when x=3 also go to the place 1 – user45415631 Jul 19 '14 at 14:30
  • In your results you have 3 1 1 1 1 1 1 2 2 2. So we have a new order, and I need to change the numeration: 1,2,2,2,2,2,2,3,3,3 – user45415631 Jul 19 '14 at 14:36
  • 1
    `user45415631`. I am not sure I understand. You wanted the result to be `3 1 1 1 1 1 1 2 2 2`, right?. Please show an example and the expected result you wanted. x – akrun Jul 19 '14 at 14:40
  • `user45415631`. I just updated the code. Please check if this is what you wanted. – akrun Jul 19 '14 at 14:51
  • Thank you, that is exactly what I need! But how can I do if I have table (data.frame) with column X and many other columns like Z ? – user45415631 Jul 19 '14 at 22:55
0

If I'm understanding correctly, it sounds as though you want to define your own order for sorting something. Is that right? Two ways you could do that:

Option #1: Make another column in your data.frame and assign values in the order you'd like. If you wanted the threes to come first, the ones to come second and the twos to come third, you'd do this:

 Data$y <- rep(NA, nrow(Data)
 Data$y[Data$x == 3] <- 1
 Data$y[Data$x == 1] <- 2
 Data$y[Data$x == 2] <- 3

Then you can sort on y and your data.frame will have the order you want.

Option #2: If the numbers you list in x are levels in a factor, you could do this using plyr:

 library(plyr)
 Data$x <- revalue(Data$x, c("3" = "1", "1" = "2", "2" = "3"))

Personally, I think that the 2nd option would be rather confusing, but if you are using "1", "2", and "3" to refer to levels in a factor, that is one quick way to change things.

shirewoman2
  • 1,693
  • 1
  • 17
  • 27
  • Thank you! If I change the order in column X, do all other rows in table change the order. For example, if we change 3 to 1, I need all the rows when x=3 also go to the place 1. – user45415631 Jul 19 '14 at 14:07
  • And what is Y in version I – user45415631 Jul 19 '14 at 14:42
  • Yes, if you sort one column in a data.frame, R will automatically sort all the others. – shirewoman2 Jul 19 '14 at 15:18
  • And "y" in version 1 is the new column in your data.frame that you can sort by. Once you create that new column y, you can sort like this: `Data – shirewoman2 Jul 19 '14 at 15:20
  • Ok, thanks! In your version you just change the order of the rows. But I mean the following: the number 3 in X need to be changed to number 1, number 1 changed to number 2 and number 2 changed to number 3 along with all rows. The output: result= (1,2,2,2,2,2,2,3,3,3). Sorry if it was unclear at first time ) – user45415631 Jul 19 '14 at 16:16