I have a named list with tables (1 or 2 columns).
mlst=list(test1 = table(c(rep("x", 12), rep("t", 10))),
test2 = table(c(rep("y", 20), rep("z", 5))),
test3 = table(c(rep("w", 30), rep("m", 50))))
$test1
t x
10 12
$test2
y z
20 5
$test3
m w
50 30
do.call(rbind, mlst)
t x
test1 10 12
test2 20 5
test3 50 30
As you can see, the columns names are t and x, but each title is different.
Here you see it's easy to get the names of the list.
names(mlst)
[1] "test1" "test2" "test3"
But how can I get the names of the columns of the tables in a new column?
v1 v2 v1t v2t
test1 10 12 t x
test2 20 5 y z
test3 50 30 m w
Same as above but a data frame has 1 column, the other 2.
mlst2=list(test1 = table(c(rep("x", 20), rep("t", 10))),
test2 = table(c(rep("z", 30))),
test3 = table(c(rep("w", 40), rep("m", 60))))
do.call(rbind, mlst2)
t x
test1 10 20
test2 30 30
test3 60 40
But you see that this last command prints non-sense for test2 (recycling the z var, in this case it should be test2 30 NA z NA
)
I'm trying to avoid for loops for efficiency.