thanks for the great help! But I have one additional question (hope I can work alone then); I want to replace the values of a vector, like this: x <- c(1,2,2,3,3,4,4,4,5,1) x[x==1] <- c(12) x[x==2] <- c(13) x[x==3] <- c(17) x[x==4] <- c(20) x[x==5] <- c(22) is there a way to do that just in one or two code lines? I tried x[x == c(1:5)] <- c(12,13,17,20,22) but that makes something totally different ... thanks for any help!
converting multiple variable numbers
4 messages · Jörg Groß, Jorge Ivan Velez, David Winsemius +1 more
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090117/4a89c2f2/attachment-0001.pl>
On Jan 17, 2009, at 12:47 PM, J?rg Gro? wrote:
thanks for the great help! But I have one additional question (hope I can work alone then); I want to replace the values of a vector, like this: x <- c(1,2,2,3,3,4,4,4,5,1) x[x==1] <- c(12) x[x==2] <- c(13) x[x==3] <- c(17) x[x==4] <- c(20) x[x==5] <- c(22) is there a way to do that just in one or two code lines?
recode.mtx <- matrix( c(1,12, 2,13, 3,17, 4,20, 5,22), ncol=2, byrow=TRUE) # could also use matrix( c(1:5, 12, 13, 17, 20, 22), ncol=2) x <- sapply(x, function(x) recode.mtx[x,2] ) > x [1] 12 13 13 17 17 20 20 20 22 12
David Winsemius > > > > I tried > > x[x == c(1:5)] <- c(12,13,17,20,22) > > but that makes something totally different ... > > > thanks for any help! > > ______________________________________________ > 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.
Here is yet another way of doing it:
x <- c(1,2,2,3,3,4,4,4,5,1)
cvt <- c('1'=12, '2'=13, '3'=17, '4'=20, '5'=22)
cvt[as.character(x)]
1 2 2 3 3 4 4 4 5 1 12 13 13 17 17 20 20 20 22 12
On Sat, Jan 17, 2009 at 12:47 PM, J?rg Gro? <joerg at licht-malerei.de> wrote:
thanks for the great help! But I have one additional question (hope I can work alone then); I want to replace the values of a vector, like this: x <- c(1,2,2,3,3,4,4,4,5,1) x[x==1] <- c(12) x[x==2] <- c(13) x[x==3] <- c(17) x[x==4] <- c(20) x[x==5] <- c(22) is there a way to do that just in one or two code lines? I tried x[x == c(1:5)] <- c(12,13,17,20,22) but that makes something totally different ... thanks for any help!
______________________________________________ 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 that you are trying to solve?