I am doing a school project of data cleaning with tidyverse
package. Now I get a list output from purrr::map()
like this:
(mylist <- list(A = as.Date(sample(1e3:1e4, 4), origin = "1960-01-01"),
B = as.Date(sample(1e3:1e4, 2), origin = "1960-01-01"),
C = as.Date(sample(1e3:1e4, 3), origin = "1960-01-01")))
$A
[1] "1970-06-12" "1984-05-28" "1967-06-28" "1982-12-14"
$B
[1] "1966-02-04" "1967-02-21"
$C
[1] "1977-07-19" "1968-03-11" "1964-02-13"
I want to stack them to:
df <- data.frame(Value = reduce(mylist, c))
df$Class <- rep(names(mylist), sapply(mylist, length))
df
Value Class
1 1970-06-12 A
2 1984-05-28 A
3 1967-06-28 A
4 1982-12-14 A
5 1966-02-04 B
6 1967-02-21 B
7 1977-07-19 C
8 1968-03-11 C
9 1964-02-13 C
- Note1: The length of every cell is different and the values in the list are
Date
class actually. - Note2:
stack(mylist)
doesn't work in Date list.
Are there any methods to achieve it efficiently with functions in tidyverse
or other packages?