Mohammad Tanvir Ahamed
2016-Mar-08 00:47 UTC
[R] Extract every 2 element for a list which are not equal in length
Hi, a <- c(1:5)b <- c(1:3) c <- 1 d <- 5 e <- list(a,b,c,d) # To extract every 1st element lapply(e,"[[",1) ## Out-put [[1]] [1] 1 [[2]] [1] 1 [[3]] [1] 1 [[4]] [1] 5 #To extract every 2nd element (Need help in this case) lapply(e,"[[",2) ## Expected outcome [[1]] [1] 2 [[2]] [1] 2 [[3]] [1] NA [[4]] [1] NA Any help will be appreciated . Thanks Tanvir Ahamed G?teborg, Sweden | mashranga at yahoo.com
Jim Lemon
2016-Mar-08 02:00 UTC
[R] Extract every 2 element for a list which are not equal in length
Hi Tanvir, I think what you want is: lapply(e,"[",1) lapply(e,"[",2) Jim On Tue, Mar 8, 2016 at 11:47 AM, Mohammad Tanvir Ahamed via R-help <r-help at r-project.org> wrote:> Hi, > > a <- c(1:5)b <- c(1:3) > c <- 1 > d <- 5 > e <- list(a,b,c,d) > > # To extract every 1st element > lapply(e,"[[",1) > > ## Out-put > [[1]] > [1] 1 > > [[2]] > [1] 1 > > [[3]] > [1] 1 > > [[4]] > [1] 5 > > #To extract every 2nd element (Need help in this case) > lapply(e,"[[",2) > > ## Expected outcome > [[1]] > [1] 2 > > [[2]] > [1] 2 > > [[3]] > [1] NA > > [[4]] > [1] NA > > > > Any help will be appreciated . Thanks > > > Tanvir Ahamed > G?teborg, Sweden | mashranga at yahoo.com > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Jorge I Velez
2016-Mar-08 02:04 UTC
[R] Extract every 2 element for a list which are not equal in length
Dear Mohammad, What's wrong with the result? Best, Jorge.- On Monday, March 7, 2016, Mohammad Tanvir Ahamed via R-help < r-help at r-project.org> wrote:> Hi, > > a <- c(1:5)b <- c(1:3) > c <- 1 > d <- 5 > e <- list(a,b,c,d) > > # To extract every 1st element > lapply(e,"[[",1) > > ## Out-put > [[1]] > [1] 1 > > [[2]] > [1] 1 > > [[3]] > [1] 1 > > [[4]] > [1] 5 > > #To extract every 2nd element (Need help in this case) > lapply(e,"[[",2) > > ## Expected outcome > [[1]] > [1] 2 > > [[2]] > [1] 2 > > [[3]] > [1] NA > > [[4]] > [1] NA > > > > Any help will be appreciated . Thanks > > > Tanvir Ahamed > G?teborg, Sweden | mashranga at yahoo.com <javascript:;> > > ______________________________________________ > R-help at r-project.org <javascript:;> mailing list -- To UNSUBSCRIBE and > more, see > 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.-- Sent from my phone. Please excuse my brevity and misspelling. [[alternative HTML version deleted]]
Mohammad Tanvir Ahamed
2016-Mar-08 02:27 UTC
[R] Extract every 2 element for a list which are not equal in length
Hi Jim, Thanks . Some how i have messed up mu lapply function. Worked upon restart. Tanvir Ahamed G?teborg, Sweden | mashranga at yahoo.com ----- Original Message ----- From: Jim Lemon <drjimlemon at gmail.com> To: Mohammad Tanvir Ahamed <mashranga at yahoo.com> Cc: R-help Mailing List <r-help at r-project.org> Sent: Tuesday, 8 March 2016, 3:00 Subject: Re: [R] Extract every 2 element for a list which are not equal in length Hi Tanvir, I think what you want is: lapply(e,"[",1) lapply(e,"[",2) Jim On Tue, Mar 8, 2016 at 11:47 AM, Mohammad Tanvir Ahamed via R-help <r-help at r-project.org> wrote:> Hi, > > a <- c(1:5)b <- c(1:3) > c <- 1 > d <- 5 > e <- list(a,b,c,d) > > # To extract every 1st element > lapply(e,"[[",1) > > ## Out-put > [[1]] > [1] 1 > > [[2]] > [1] 1 > > [[3]] > [1] 1 > > [[4]] > [1] 5 > > #To extract every 2nd element (Need help in this case) > lapply(e,"[[",2) > > ## Expected outcome > [[1]] > [1] 2 > > [[2]] > [1] 2 > > [[3]] > [1] NA > > [[4]] > [1] NA > > > > Any help will be appreciated . Thanks > > > Tanvir Ahamed > G?teborg, Sweden | mashranga at yahoo.com > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Mohammad Tanvir Ahamed
2016-Mar-08 02:27 UTC
[R] Extract every 2 element for a list which are not equal in length
Hi Jorge,? Thanks . Some how i have messed up mu lapply function.Worked upon restart.??Tanvir Ahamed G?teborg, Sweden | mashranga at yahoo.com From: Jorge I Velez <jorgeivanvelez at gmail.com> To: Mohammad Tanvir Ahamed <mashranga at yahoo.com> Cc: R-help Mailing List <r-help at r-project.org> Sent: Tuesday, 8 March 2016, 3:04 Subject: Re: [R] Extract every 2 element for a list which are not equal in length Dear Mohammad,What's wrong with?the result?Best,Jorge.- On Monday, March 7, 2016, Mohammad Tanvir Ahamed via R-help <r-help at r-project.org> wrote: Hi, a <- c(1:5)b <- c(1:3) c <- 1 d <- 5 e <- list(a,b,c,d) # To extract every 1st element lapply(e,"[[",1) ## Out-put [[1]] [1] 1 [[2]] [1] 1 [[3]] [1] 1 [[4]] [1] 5 #To extract every 2nd element (Need help in this case) lapply(e,"[[",2) ## Expected outcome [[1]] [1] 2 [[2]] [1] 2 [[3]] [1] NA [[4]] [1] NA Any help will be appreciated . Thanks Tanvir Ahamed G?teborg, Sweden? |? mashranga at yahoo.com ______________________________________________ R-help at r-project.org?mailing list -- To UNSUBSCRIBE and more, see 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. -- Sent from my phone. Please excuse my brevity and misspelling. [[alternative HTML version deleted]]