list with list function

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

list with list function

R help mailing list-2
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 

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: list with list function

Rui Barradas
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
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: list with list function

Ivan Krylov
In reply to this post by R help mailing list-2
On Mon, 4 Feb 2019 21:01:06 +0000 (UTC)
Andras Farkas via R-help <[hidden email]> 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: list with list function

R help mailing list-2
In reply to this post by Rui Barradas
Thanks Rui and Ivan, works perfectly...
Andras
    On Monday, February 4, 2019, 4:18:39 PM EST, Rui Barradas <[hidden email]> 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
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.