I have a list like this:
arg0 <- list(code = "a", n = rep(10, 3))
The number of objects in a list is variable. The objects of the list are vectors — only one dimensional objects.
I want to make a procedure to convert the list to a one row data.frame like this:
> data.frame(code = "a", n.1 = 10, n.2 = 10, n.3 = 10)
code n.1 n.2 n.3
1 a 10 10 10
I have this solution currently:
a <- stack(arg0)
b <- data.frame(t(a[,1]))
names(b) <- a[,2]
b <- data.frame(b)
Where b is almost the result I want to achieve:
> b
code n n.1 n.2
1 a 10 10 10
Two questions:
- Is there more elegant way to achieve the result?
- Do you have any idea how to get the numbering of the duplicate colnames like
c(n.1,?
n.2, n.3)
If you want to keep the different data types of the list’s objects, the following command can be used:
The output:
The values in columns two to four are still numeric.