Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recoding Numeric Vector R

Tags:

r

I have a numeric vector, let's say something like:

x <- rep(1:6, 300)

What I would like to do is recode the vector, in place such that 6=1,5=2,4=3,3=4,2=5,1=6. I don't want to create a factor out of it.

Everything I have tried so far gives me the wrong counts because of the order, ie:

x[x == 6] <- 1 
x[x == 5] <- 2 ## Lines that follow where x[x == 2] removes 5's entirely from counts. 

Note: I'm aware of the car package, but would prefer to use base R for this problem.

like image 857
Brandon Bertelsen Avatar asked Feb 01 '11 03:02

Brandon Bertelsen


1 Answers

Construct a map between the old and new values, and subset with the old,

(6:1)[x]
like image 53
Martin Morgan Avatar answered Sep 22 '22 11:09

Martin Morgan