r - Replace all values of a recursive list with values of a vector -
say, have following recursive list:
rec_list <- list(list(rep(1,5), 10), list(rep(100, 4), 20:25)) rec_list [[1]] [[1]][[1]] [1] 1 1 1 1 1 [[1]][[2]] [1] 10 [[2]] [[2]][[1]] [1] 100 100 100 100 [[2]][[2]] [1] 20 21 22 23 24 25
now, replace values of list, say, vector seq_along(unlist(rec_list))
, , keep structure of list. tried using empty index subsetting like
rec_list[] <- seq_along(unlist(rec_list))
but doesn't work.
how can achieve replacement while keeping original structure of list?
you can use relist
:
relist(seq_along(unlist(rec_list)), skeleton = rec_list) # [[1]] # [[1]][[1]] # [1] 1 2 3 4 5 # # [[1]][[2]] # [1] 6 # # # [[2]] # [[2]][[1]] # [1] 7 8 9 10 # # [[2]][[2]] # [1] 11 12 13 14 15 16
Comments
Post a Comment