Reputation: 788
a,b and c are list.
a<-list(c(6,5,7),c(1,2),c(1,3,4))
b<-list(c(1,2,3),c(4,5),c(6,7,8))
c<-list(1,2,2)
I want to replace "a" with "b" at place "c" to generate a new list.
The expected result is as follows:
[[1]]
[1] 1 5 7
[[2]]
[1] 1 5
[[3]]
[1] 1 7 4
Thank you for help!
Upvotes: 4
Views: 2733
Reputation: 12005
For the R newbies that find that find the syntax of Map
daunting:
for(i in seq(c)){
a[[i]][unlist(c[i])] <- b[[i]][unlist(c[i])]
}
a
# [[1]]
# [1] 1 5 7
#
# [[2]]
# [1] 1 5
#
# [[3]]
# [1] 1 7 4
Upvotes: 0
Reputation: 206566
Sounds like you're looking for Map
to iterate over all the lists simultaneously.
Map(function(a,b,c) {a[c]<-b[c]; a},a,b,c)
# [[1]]
# [1] 1 5 7
#
# [[2]]
# [1] 1 5
#
# [[3]]
# [1] 1 7 4
Upvotes: 8