Reputation: 13334
I am trying to create the data structure shown here (read in from RJSONIO). I need to produce this structure in R from raw data. As you can see it is a data frame of 4 variables, where the 4th variable (children) is a list of data frames. I am having trouble finding a way to create a data frame that contains a list as one of its variables.
Data Structure as read in from RJSONIO (rjsonio_frame)
'data.frame': 5 obs. of 4 variables:
$ name : chr "" "" "" "" ...
$ imageURL: chr "images/failure.png" "images/failure.png" "images/failure.png" "images/failure.png" ...
$ id : chr "2" "11" "20" "29" ...
$ children:List of 5
..$ :'data.frame': 8 obs. of 3 variables:
.. ..$ name : chr "word 1" "word 2" "word 3" "word 4" ...
.. ..$ imageURL: chr "" "" "" "" ...
.. ..$ id : chr "3" "4" "5" "6" ...
..$ :'data.frame': 8 obs. of 3 variables:
.. ..$ name : chr "word 1" "word 2" "word 3" "word 4" ...
.. ..$ imageURL: chr "" "" "" "" ...
.. ..$ id : chr "12" "13" "14" "15" ...
..$ :'data.frame': 8 obs. of 3 variables:
.. ..$ name : chr "word 1" "word 2" "word 3" "word 4" ...
.. ..$ imageURL: chr "" "" "" "" ...
.. ..$ id : chr "21" "22" "23" "24" ...
..$ :'data.frame': 8 obs. of 3 variables:
.. ..$ name : chr "word 1" "word 2" "word 3" "word 4" ...
.. ..$ imageURL: chr "" "" "" "" ...
.. ..$ id : chr "30" "31" "32" "33" ...
..$ :'data.frame': 8 obs. of 3 variables:
.. ..$ name : chr "word 1" "word 2" "word 3" "word 4" ...
.. ..$ imageURL: chr "" "" "" "" ...
.. ..$ id : chr "39" "40" "41" "42" ...
I can create a list of data frames easily (the imageURL is blank):
list_of_frames
[[1]]
name imageURL id
1 word1 3
2 word2 4
3 word3 5
4 word4 6
5 word5 7
[[2]]
name imageURL id
1 word1 8
2 word2 9
3 word3 10
4 word4 11
5 word5 12
[[3]]
name imageURL id
1 word1 13
2 word2 14
3 word3 15
4 word4 16
5 word5 17
But how can I add this list to the character variable of the top most data frame (assume I can already make the rest of the required data frame)? I have tried this:
final_frame <- data.frame(name=name_vector, imageURL=image_vector, id=id_vector, children=list_of_frames)
The name, imageURL, and id all go into the data frame correctly, but the list_of_frames does not. If I will subset the CORRECT version from RSJONIO here (names are blank):
rjsonio_frame[[4]]
name imageURL id
1 images/failure.png 2
2 images/failure.png 11
3 images/failure.png 20
4 images/failure.png 29
5 images/failure.png 38
children
1 word 1, word 2, word 3, word 4, word 5, word 6, word 7, word 8, , , , , , , , , 3, 4, 5, 6, 7, 8, 9, 10
2 word 1, word 2, word 3, word 4, word 5, word 6, word 7, word 8, , , , , , , , , 12, 13, 14, 15, 16, 17, 18, 19
3 word 1, word 2, word 3, word 4, word 5, word 6, word 7, word 8, , , , , , , , , 21, 22, 23, 24, 25, 26, 27, 28
Not the appearance of the children variable. I subset this further I get:
rjsonio_frame[[4]][[4]]
[[1]]
name imageURL id
1 word 1 3
2 word 2 4
3 word 3 5
4 word 4 6
5 word 5 7
6 word 6 8
7 word 7 9
8 word 8 10
[[2]]
name imageURL id
1 word 1 12
2 word 2 13
3 word 3 14
4 word 4 15
5 word 5 16
6 word 6 17
7 word 7 18
8 word 8 19
[[3]]
name imageURL id
1 word 1 21
2 word 2 22
3 word 3 23
4 word 4 24
5 word 5 25
6 word 6 26
7 word 7 27
8 word 8 28
This looks like my list_of_frames. But my final_frame does not show this.
Upvotes: 0
Views: 54
Reputation: 7232
Just put it outside the data.frame initialization, so data.frame() will not unlist the children:
final_frame <- data.frame(
name=name_vector,
imageURL=image_vector,
id=id_vector)
final_frame$children <- list_of_frames
EDIT (for sake of completeness):
... or as akrun suggested - you can inhibit conversion by
final_frame <- data.frame(
name=name_vector,
imageURL=image_vector,
id=id_vector,
children = I(list_of_frames))
Upvotes: 1