I've been researching for quite a while but haven't found a convenient answer to this problem so I am proposing new question although it looks quite simple.
I have a column (gained by using str_extract_all
function that returns all occurences of regex in string) which contains 0 to multiple items like below:
list(c("1/3", "2327/1", "2312/3", "2312/7", "2327/3", "2327/2" ), c("10655/1", "5/2024"), c("1690/1", "1693/2", "1688/5", "721/66" ), c("3675/1", "4233/1", "4232/1", "4232/2", "4235/1", "4235/2" ), c("109/9", "109/10"), c("3042/5", "3042/7", "3043/1", "3043/2", "3043/3", "1444/4", "1444/5", "1444/6", "2202/3", "3043/3"), "336/15", "1/3", "1/2", c("1/2", "340/5"))
So it combines vectors and individual items.
Now I want to split my_column into multiple columns that contain only the single items (number of new columns being the max length of the nested vectors). How can this be achieved?
If your data is a list named dat
you can do:
n <- lengths(dat)
max_n <- max(n)
as.data.frame(do.call(rbind, lapply(1:length(n), \(i) c(dat[[i]], rep(NA, times=max_n-n[i])))))
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1 1/3 2327/1 2312/3 2312/7 2327/3 2327/2 <NA> <NA> <NA> <NA>
2 10655/1 5/2024 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
3 1690/1 1693/2 1688/5 721/66 <NA> <NA> <NA> <NA> <NA> <NA>
4 3675/1 4233/1 4232/1 4232/2 4235/1 4235/2 <NA> <NA> <NA> <NA>
5 109/9 109/10 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
6 3042/5 3042/7 3043/1 3043/2 3043/3 1444/4 1444/5 1444/6 2202/3 3043/3
7 336/15 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
8 1/3 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
9 1/2 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
10 1/2 340/5 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>