[R] Compare each element of a list to a vector
Hello R-helpers, I have a vector x-c(1,2,3) and a list that contains vectors datalist-list(c(1,2,3),c(2,3,4),c(3,4,5),c(4,5,6)) and I would like to identify those list elements that are identical to x. I tried datalist %in% x [1] FALSE FALSE FALSE FALSE but I am obviously using %in% incorrectly. I also tried messing around with lapply but I can't figure out how to specify the function within lapply. I would appreciate any suggestions you may have. Many thanks! Mark Na [[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.
Re: [R] Compare each element of a list to a vector
try this: x-c(1,2,3) datalist-list(c(1,2,3),c(2,3,4),c(3,4,5),c(4,5,6)) result - sapply(datalist, function(.vec){ + all(.vec == x) + }) result [1] TRUE FALSE FALSE FALSE On Sun, Feb 3, 2013 at 1:15 PM, mtb...@gmail.com wrote: Hello R-helpers, I have a vector x-c(1,2,3) and a list that contains vectors datalist-list(c(1,2,3),c(2,3,4),c(3,4,5),c(4,5,6)) and I would like to identify those list elements that are identical to x. I tried datalist %in% x [1] FALSE FALSE FALSE FALSE but I am obviously using %in% incorrectly. I also tried messing around with lapply but I can't figure out how to specify the function within lapply. I would appreciate any suggestions you may have. Many thanks! Mark Na [[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. -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. __ 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.
Re: [R] Compare each element of a list to a vector
Try datalist %in% list(x) [1] TRUE FALSE FALSE FALSE Both arguments, e1 and e2, of e1 %in% e2 should be of the same type: e1 %in% e2 is comparing e1[i] and e2[j]. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of mtb...@gmail.com Sent: Sunday, February 03, 2013 10:15 AM To: r-help@r-project.org Subject: [R] Compare each element of a list to a vector Hello R-helpers, I have a vector x-c(1,2,3) and a list that contains vectors datalist-list(c(1,2,3),c(2,3,4),c(3,4,5),c(4,5,6)) and I would like to identify those list elements that are identical to x. I tried datalist %in% x [1] FALSE FALSE FALSE FALSE but I am obviously using %in% incorrectly. I also tried messing around with lapply but I can't figure out how to specify the function within lapply. I would appreciate any suggestions you may have. Many thanks! Mark Na [[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. __ 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.
Re: [R] Compare each element of a list to a vector
My attempt similar to Jim's is: which(sapply(datalist, function(z) all(z == x))) However, a safer approach is: which(sapply(datalist, function(z) isTRUE(all.equal(z, x This latter approach avoids Circle 1 of 'The R Inferno'. http://www.burns-stat.com/documents/books/the-r-inferno/ Pat On 03/02/2013 18:24, jim holtman wrote: try this: x-c(1,2,3) datalist-list(c(1,2,3),c(2,3,4),c(3,4,5),c(4,5,6)) result - sapply(datalist, function(.vec){ + all(.vec == x) + }) result [1] TRUE FALSE FALSE FALSE On Sun, Feb 3, 2013 at 1:15 PM, mtb...@gmail.com wrote: Hello R-helpers, I have a vector x-c(1,2,3) and a list that contains vectors datalist-list(c(1,2,3),c(2,3,4),c(3,4,5),c(4,5,6)) and I would like to identify those list elements that are identical to x. I tried datalist %in% x [1] FALSE FALSE FALSE FALSE but I am obviously using %in% incorrectly. I also tried messing around with lapply but I can't figure out how to specify the function within lapply. I would appreciate any suggestions you may have. Many thanks! Mark Na [[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. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ 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.
Re: [R] Compare each element of a list to a vector
Thanks Jim, William and Patrick for your ideas. I appreciate your help. Avoiding a circle of the R Inferno sounds good, so I'm going to use Patrick's 2nd suggestion for now but I learned something from the others too. Cheers, Mark On Sun, Feb 3, 2013 at 12:33 PM, Patrick Burns pbu...@pburns.seanet.comwrote: My attempt similar to Jim's is: which(sapply(datalist, function(z) all(z == x))) However, a safer approach is: which(sapply(datalist, function(z) isTRUE(all.equal(z, x This latter approach avoids Circle 1 of 'The R Inferno'. http://www.burns-stat.com/**documents/books/the-r-inferno/http://www.burns-stat.com/documents/books/the-r-inferno/ Pat On 03/02/2013 18:24, jim holtman wrote: try this: x-c(1,2,3) datalist-list(c(1,2,3),c(2,3,**4),c(3,4,5),c(4,5,6)) result - sapply(datalist, function(.vec){ + all(.vec == x) + }) result [1] TRUE FALSE FALSE FALSE On Sun, Feb 3, 2013 at 1:15 PM, mtb...@gmail.com wrote: Hello R-helpers, I have a vector x-c(1,2,3) and a list that contains vectors datalist-list(c(1,2,3),c(2,3,**4),c(3,4,5),c(4,5,6)) and I would like to identify those list elements that are identical to x. I tried datalist %in% x [1] FALSE FALSE FALSE FALSE but I am obviously using %in% incorrectly. I also tried messing around with lapply but I can't figure out how to specify the function within lapply. I would appreciate any suggestions you may have. Many thanks! Mark Na [[alternative HTML version deleted]] __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/**blog http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') [[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.