Repeat objects in list in R -
i have 2 list files , b:
a<-list(1,5,8,2,6,4,9,5) b<-list(2,2,3,2,4,1,3,1)
i want every object in list repeat "matched objects of b" times, respectively. expected result follows:
[[1]] [1] 1 1 [[2]] [1] 5 5 [[3]] [1] 8 8 8 [[4]] [1] 2 2 [[5]] [1] 6 6 6 6 [[6]] [1] 4 [[7]] [1] 9 9 9 [[8]] [1] 5
i use code: lapply(1:length(a), function(x) {rep(a[[x]],b[[x]])})
but want explore other ways. thank help!
you can use map
!
map(rep, a, b)
Comments
Post a Comment