Dear R-Helpers: I am a entry level user of R. Have the following question. Many thanks in advance. # 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') # 3 2 2 1 # I try to avoid using the following loop to achieve the goal because the which.vec in reality will be very long vector.I.want <- rep(NA,length(which.vec)) for (i in 1:length(which.vec)) { vector.I.want[i] <- value.vec[which.vec[i]] } # is there a faster way than looping? Thanks in advance. -Sean [[alternative HTML version deleted]]
David Winsemius
2008-Dec-24 03:09 UTC
[R] Extract values based on indexes without looping?
> 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]] }
Jorge Ivan Velez
2008-Dec-24 03:12 UTC
[R] Extract values based on indexes without looping?
How about this? value.vec[which.vec] HTH, Jorge On Tue, Dec 23, 2008 at 9:52 PM, Sean Zhang <seanecon@gmail.com> wrote:> Dear R-Helpers: > > I am a entry level user of R. > > Have the following question. Many thanks in advance. > > > # 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') > # 3 2 2 1 > > > # I try to avoid using the following loop to achieve the goal because the > which.vec in reality will be very long > > vector.I.want <- rep(NA,length(which.vec)) > for (i in 1:length(which.vec)) > { vector.I.want[i] <- value.vec[which.vec[i]] } > > # is there a faster way than looping? > > Thanks in advance. > > -Sean > > [[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]]
Charles C. Berry
2008-Dec-24 03:14 UTC
[R] Extract values based on indexes without looping?
On Tue, 23 Dec 2008, Sean Zhang wrote:> Dear R-Helpers: > > I am a entry level user of R. >You need to read An Introduction to R particularly this section: 2.7 Index vectors; selecting and modifying subsets of a data set ... 2. A vector of positive integral quantities Also, see ?Subscript and run example( Subscript ) HTH, Chuck> Have the following question. Many thanks in advance. > > > # 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') > # 3 2 2 1 > > > # I try to avoid using the following loop to achieve the goal because the > which.vec in reality will be very long > > vector.I.want <- rep(NA,length(which.vec)) > for (i in 1:length(which.vec)) > { vector.I.want[i] <- value.vec[which.vec[i]] } > > # is there a faster way than looping? > > Thanks in advance. > > -Sean > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901