Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace values of a list with another in R

Tags:

list

r

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!

like image 471
lightsnail Avatar asked Feb 08 '23 19:02

lightsnail


1 Answers

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
like image 97
MrFlick Avatar answered Feb 16 '23 04:02

MrFlick