Creating a new list by combining element of previous list in R -
i have list this:
v = list(a1= 1, a2 = 2, b1 = 3, b2= 4, b3 = 5)
my desire result creating list :
v = list(a = c(1, 2), b = c(3, 4, 5))
but should mentioned vector v
exapmle, can wrote code above example, problem if each time length of ai
, bi
is different, how can wrote function desire result in r?
split(unlist(v), sub("\\d+$", "", names(v))) # $a # a1 a2 # 1 2 # # $b # b1 b2 b3 # 3 4 5
Comments
Post a Comment