Message-ID: <9572BA04-BA6C-4490-9851-6DCEEE2BB9B4@comcast.net>
Date: 2008-12-24T03:09:43Z
From: David Winsemius
Subject: Extract values based on indexes without looping?
In-Reply-To: <97c8246d0812231852n40485983w3dbeebd4598df8f3@mail.gmail.com>
> sapply(which.vec, function(x){ value.vec[x] } )
[1] "c" "b" "b" "a"
On Dec 23, 2008, at 9:52 PM, Sean Zhang wrote:
> # value.vec stores values
> value.vec <- c('a','b','c')
> # which.vec stores the locations/indexs of values in value.vec.
> which.vec <- c(3, 2, 2, 1)
> # How can I obtain the following vector based on the value.vec and
> which.vec
> mentioned above
> # vector.I.want <- c('c', 'b', 'b', 'a')
> vector.I.want <- rep(NA,length(which.vec))
> for (i in 1:length(which.vec))
> { vector.I.want[i] <- value.vec[which.vec[i]] }