Reputation: 7517
I was wondering if there might be a smart way to make the output of eval(x2)
become a list
exactly like the output of eval(x1)
without changing x2
?
P.S. My ultimate goal is to achieve a function for this output reformatting.
x1 = list(dbinom(1:5, 5, .1), dbinom(1:7, 5, .1))
eval(x1) ## this list format output is desired
x2 = c(dbinom(1:5, 5, .1), dbinom(1:7, 5, .1))
eval(x2) ## can make this output to become like that of `eval(x1)`?
Upvotes: 0
Views: 35
Reputation: 389135
We can name the vector and then split
?
x2 <- c(a = dbinom(1:5, 5, .1), b = dbinom(1:7, 5, .1))
split(x2, substr(names(x2), 1, 1))
#$a
# a1 a2 a3 a4 a5
#0.32805 0.07290 0.00810 0.00045 0.00001
#$b
# b1 b2 b3 b4 b5 b6 b7
#0.32805 0.07290 0.00810 0.00045 0.00001 0.00000 0.00000
Upvotes: 1