Hello everyone, wonder if you would have a thought on a function for the following: we have a<-sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"),5) b<-sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"), 4) c<-sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"), 3) d<-c(1,3,5) e<-c(1,4) f<-c(1,2) listA<-list(a,b,c) listB<-list(d,e,f) what I would like to do with a function (my real listA and listB can be of any length but always equal length, but their components like a,b,and c those can be unequal) as opposed to manually is to derive the following answer listfinal<-list(a[-d],b[-e],c[-f]) listfinal essentially the elements in listB serve as identifying the position of corresponding list element in listA and removing it from listA.? these lists listA and listB in practice are columns of a data frame that I am trying to work with and were generated with a function using lapply... appreciate any thoughts you may have to make this functional... thanks, Andras?
Hello, Like this? Map('[', listA, lapply(listB, '*', -1)) Hope this helps, Rui Barradas ?s 21:01 de 04/02/2019, Andras Farkas via R-help escreveu:> Hello everyone, > > wonder if you would have a thought on a function for the following: > > > we have > > a<-sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"),5) > b<-sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"), 4) > c<-sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"), 3) > > d<-c(1,3,5) > e<-c(1,4) > f<-c(1,2) > > listA<-list(a,b,c) > listB<-list(d,e,f) > > > what I would like to do with a function (my real listA and listB can be of any length but always equal length, but their components like a,b,and c those can be unequal) as opposed to manually is to derive the following answer > > listfinal<-list(a[-d],b[-e],c[-f]) > listfinal > > > essentially the elements in listB serve as identifying the position of corresponding list element in listA and removing it from listA. > > these lists listA and listB in practice are columns of a data frame that I am trying to work with and were generated with a function using lapply... > > appreciate any thoughts you may have to make this functional... > > thanks, > > Andras > > ______________________________________________ > 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. >
On Mon, 4 Feb 2019 21:01:06 +0000 (UTC) Andras Farkas via R-help <r-help at r-project.org> wrote:> listA<-list(a,b,c) > listB<-list(d,e,f) > > what I would like to do with a function <...> as opposed to manually > is to derive the following answer > > listfinal<-list(a[-d],b[-e],c[-f])The `Map` function, unlike `lapply`, iterates over its arguments simultaneously: Map(function(dt, idx) dt[-idx], listA, listB) # [[1]] # [1] "1999-07-31" "1999-06-12" # # [[2]] # [1] "1999-03-10" "1999-04-04" # # [[3]] # [1] "1999-08-07" -- Best regards, Ivan
Thanks Rui and Ivan, works perfectly... Andras On Monday, February 4, 2019, 4:18:39 PM EST, Rui Barradas <ruipbarradas at sapo.pt> wrote: Hello, Like this? Map('[', listA, lapply(listB, '*', -1)) Hope this helps, Rui Barradas ?s 21:01 de 04/02/2019, Andras Farkas via R-help escreveu:> Hello everyone, > > wonder if you would have a thought on a function for the following: > > > we have > > a<-sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"),5) > b<-sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"), 4) > c<-sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"), 3) > > d<-c(1,3,5) > e<-c(1,4) > f<-c(1,2) > > listA<-list(a,b,c) > listB<-list(d,e,f) > > > what I would like to do with a function (my real listA and listB can be of any length but always equal length, but their components like a,b,and c those can be unequal) as opposed to manually is to derive the following answer > > listfinal<-list(a[-d],b[-e],c[-f]) > listfinal > > > essentially the elements in listB serve as identifying the position of corresponding list element in listA and removing it from listA. > > these lists listA and listB in practice are columns of a data frame that I am trying to work with and were generated with a function using lapply... > > appreciate any thoughts you may have to make this functional... > > thanks, > > Andras > > ______________________________________________ > 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. >[[alternative HTML version deleted]]