Dear List, please provide some input on the following: we have a <-c(0,1,2,3) b <-c(4,5,6,7) d <-cbind(a,b) k <-c(0,0,2,2,3,3,2) "k" in this case consists of some values of "d[,1]" in a random sequence. What I am trying to do is to create an object "f" that would have the values of "d[,2]" in it based on "k", and again, "k" here is a vector that consists of some values of "d[,1]". Basically I am trying to match the values in "k" with their corresponding pairs in "d[,2]". So the result should look like: f <-c(4,4,6,6,7,7,6) appreciate your input Andras
I think this is what you are looking for> f <- d[match(k, d[,1]), 2] > f[1] 4 4 6 6 7 7 6 ------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Andras Farkas Sent: Thursday, July 4, 2013 1:09 PM To: r-help at r-project.org Subject: [R] help on selecting values of an object Dear List, please provide some input on the following: we have a <-c(0,1,2,3) b <-c(4,5,6,7) d <-cbind(a,b) k <-c(0,0,2,2,3,3,2) "k" in this case consists of some values of "d[,1]" in a random sequence. What I am trying to do is to create an object "f" that would have the values of "d[,2]" in it based on "k", and again, "k" here is a vector that consists of some values of "d[,1]". Basically I am trying to match the values in "k" with their corresponding pairs in "d[,2]". So the result should look like: f <-c(4,4,6,6,7,7,6) appreciate your input Andras ______________________________________________ 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.
Hi, You could use: d1<- data.frame(a,b) k1<-data.frame(a=k) library(plyr) join(k1,d1,by="a")[,2] #[1] 4 4 6 6 7 7 6 A.K. ----- Original Message ----- From: Andras Farkas <motyocska at yahoo.com> To: r-help at r-project.org Cc: Sent: Thursday, July 4, 2013 2:09 PM Subject: [R] help on selecting values of an object Dear List, please provide some input on the following: we have a <-c(0,1,2,3) b <-c(4,5,6,7) d <-cbind(a,b) k <-c(0,0,2,2,3,3,2) "k" in this case consists of some values of "d[,1]" in a random sequence. What I am trying to do is to create an object "f" that would have the values of "d[,2]" in it based on "k", and again, "k" here is a vector that consists of some values of "d[,1]". Basically I am trying to match the values in "k" with their corresponding pairs in "d[,2]". So the result should look like: f <-c(4,4,6,6,7,7,6) appreciate your input Andras ______________________________________________ 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.