1

I have a dataframe with ids of participants as a first, and and their responses as json array as a second column. So it is basically a data frame with 240 observations: 240 participant codes, and 240 arrays with 13 responses in each observation that can be converted into a 3X10 array each Looking like something like that:

enter image description here

I convert json into a dataframe using jsonlite::fromJSON, and as a result I get a list of data.frames (shortened up for the simplicity):

[[1]]
   case_n  d1  d2
1      13   0 100
2      12 100   0
3      11   0 100

[[2]]
   case_n  d1  d2
1      12  50  50
2       8  50  50
3       3  50  50


[[3]]
   case_n d1 d2
1      11 50 50
2       7 50 50
3      13 50 50

I know that these can be merged into one large dataframe containing all dataframes for example using plyr::ldply

df <- ldply(converted_json, data.frame)

What is the way to correctly merge this extended df with initial participant.code?

So the wanted result would be something like;

partcode   case_n d1 d2
8pdpaqyv      11 50 50
8pdpaqyv       7 50 50
8pdpaqyv      13 50 50
3q8o3mry      11 50 50
3q8o3mry       7 50 50
3q8o3mry      13 50 50
1kueczzz      11 50 50
1kueczzz       7 50 50
1kueczzz      13 50 50

UPDATE: the task is a bit different from this question, because I also need to store the ids from the initial data.

Philipp Chapkovski
  • 1,949
  • 3
  • 22
  • 43

1 Answers1

3

dplyr::bind_rows should do what you want (it binds list elements). You have to pass additional argument for .id. To make this .id you have to name list elements.

# Name list elements
names(converted_json) <- dataOriginal$participant.code
# bind rows and add .id
library(dplyr)
bind_rows(converted_json, .id = "partcode")
pogibas
  • 27,303
  • 19
  • 84
  • 117
  • 1
    Please, don't post answers to questions that were asked hundreds of times, mark it as a duplicate. This way you will help OP to find more answers and rich discussion. – A. Suliman Aug 27 '18 at 13:47