Let's say I have the following list which I want to turn into a dataframe:
list1 = list(c("Text1","text2","text3","text4"),c("Text 1","text2","text3"),c("Text 3","text2"), c("Text 4"))
[[1]]
[1] "Text1" "text2" "text3" "text4"
[[2]]
[1] "Text 1" "text2" "text3"
[[3]]
[1] "Text 3" "text2"
[[4]]
[1] "Text 4"
To turn a list into a dataframe, I do this:
data.frame(Text = matrix(unlist(list1)), stringsAsFactors = F)
Text
1 Text1
2 text2
3 text3
4 text4
5 Text 1
6 text2
7 text3
8 Text 3
9 text2
10 Text 4
However, I lost track of which element belonged to which list. I would rather need the following output:
ID Text
1 1.1 Text1
2 1.2 text2
3 1.3 text3
4 1.4 text4
5 2.1 Text 1
6 2.2 text2
7 2.3 text3
8 3.1 Text 3
9 3.2 text2
10 4.1 Text 4
I'm stuck here unable to add another ID column where the first number indicates the number of the list and the second number denotes the sublist element in the nth list.
Can anyone help me with this?
Thanks!