Dear R user; Consider the following toy example A <- data.frame(ID1 = c(1,2,3,1,2,3,1,2,3), ID2 c("a","b","c","d","e","f","g","h","i"), stringsAsFactors = FALSE) B <- sample(a$ID2, 6, replace = TRUE) Lets say B is = "a", "a", "a", "h", "b", "e" I want to extract from A the rows where ID2 == B. If I use AA <- A[A$ID2 %in% B == TRUE,], I get only 1 row with ID2="a" instead of the 3 rows I want. Is it possible to easily implement this selection? (same row several times) Thanks Pedro [[alternative HTML version deleted]]
Try B <- c("a", "a", "a", "h", "b", "e") subset(A, ID2 %in% B) or subset(A, ID2 %in% unique(B)) will do as well -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 11/14/14, 1:45 PM, "Pedro Mardones" <mardones.p at gmail.com> wrote:>Dear R user; > >Consider the following toy example > >A <- data.frame(ID1 = c(1,2,3,1,2,3,1,2,3), ID2 >c("a","b","c","d","e","f","g","h","i"), stringsAsFactors = FALSE) >B <- sample(a$ID2, 6, replace = TRUE) > >Lets say B is = "a", "a", "a", "h", "b", "e" > >I want to extract from A the rows where ID2 == B. If I use >AA <- A[A$ID2 %in% B == TRUE,], I get only 1 row with ID2="a" instead of >the 3 rows I want. > >Is it possible to easily implement this selection? (same row several >times) > >Thanks >Pedro > > [[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.
Sorry, I was too quick. Try A[ match(B, A$ID2) ,] -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 11/14/14, 1:45 PM, "Pedro Mardones" <mardones.p at gmail.com> wrote:>Dear R user; > >Consider the following toy example > >A <- data.frame(ID1 = c(1,2,3,1,2,3,1,2,3), ID2 >c("a","b","c","d","e","f","g","h","i"), stringsAsFactors = FALSE) >B <- sample(a$ID2, 6, replace = TRUE) > >Lets say B is = "a", "a", "a", "h", "b", "e" > >I want to extract from A the rows where ID2 == B. If I use >AA <- A[A$ID2 %in% B == TRUE,], I get only 1 row with ID2="a" instead of >the 3 rows I want. > >Is it possible to easily implement this selection? (same row several >times) > >Thanks >Pedro > > [[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.
On 15/11/14 10:45, Pedro Mardones wrote:> Dear R user; > > Consider the following toy example > > A <- data.frame(ID1 = c(1,2,3,1,2,3,1,2,3), ID2 > c("a","b","c","d","e","f","g","h","i"), stringsAsFactors = FALSE) > B <- sample(a$ID2, 6, replace = TRUE) > > Lets say B is = "a", "a", "a", "h", "b", "e" > > I want to extract from A the rows where ID2 == B. If I use > AA <- A[A$ID2 %in% B == TRUE,], I get only 1 row with ID2="a" instead of > the 3 rows I want. > > Is it possible to easily implement this selection? (same row several times)AA <- A[match(B,A$ID2),] cheers, Rolf Turner P.S. The syntax "A$ID2 %in% B == TRUE" is a message brought to you by the Department of Redundancy Department, and it drives me _crazy_! Just use "A$ID2%in%B,". If "v" is a logical vector then "v==TRUE" is identical to v. See fortune(69). R. T. -- Rolf Turner Technical Editor ANZJS