Hello guys, I have a list L1 of matrix. I have another list L2 with the same number of elements representing the row of the L matrix that I want to delete (L1[[i]][-L2[[i]],]) but I can't do this with lapply as it iterates through L1 (first argument) and not L2. Any idea? ----- Anna Lippel -- View this message in context: http://n4.nabble.com/Using-lapply-with-two-lists-tp1692883p1692883.html Sent from the R help mailing list archive at Nabble.com.
Anna, See ?mapply I think you should be able to use that. Steve Riley, Pharm.D., Ph.D. Clinical Pharmacology, Specialty Neuroscience Pfizer Specialty Care Business Unit Mail Stop MS 6025-B2110 50 Pequot Ave New London, CT 06320 Steve.Riley at Pfizer.com Tel: (860) 732-1796 Fax: (860) 686-5672 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of anna Sent: Friday, March 26, 2010 5:35 PM To: r-help at r-project.org Subject: [R] Using lapply with two lists Hello guys, I have a list L1 of matrix. I have another list L2 with the same number of elements representing the row of the L matrix that I want to delete (L1[[i]][-L2[[i]],]) but I can't do this with lapply as it iterates through L1 (first argument) and not L2. Any idea? ----- Anna Lippel -- View this message in context: http://n4.nabble.com/Using-lapply-with-two-lists-tp1692883p1692883.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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 Anna, Here is a suggestion: # creating L1 L1 <- lapply(1:5, function(x) matrix(rnorm(10), ncol = 2)) L1 # creating L2 L2 <- lapply(1:5, function(x) sample(5, 2)) L2 # defining a function foo <- function(x, y) list(x[-y,]) # result mapply(foo, L1, L2) HTH, Jorge On Fri, Mar 26, 2010 at 5:34 PM, anna <> wrote:> > Hello guys, > I have a list L1 of matrix. I have another list L2 with the same number of > elements representing the row of the L matrix that I want to delete > (L1[[i]][-L2[[i]],]) but I can't do this with lapply as it iterates through > L1 (first argument) and not L2. Any idea? > > ----- > Anna Lippel > -- > View this message in context: > http://n4.nabble.com/Using-lapply-with-two-lists-tp1692883p1692883.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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]]