简体   繁体   English

将 JSON 数组转换为 r 列

[英]Convert JSON-Array to r-column

I have a quite specific question on how to convert JSON data to R.关于如何将 JSON 数据转换为 R,我有一个非常具体的问题。 I am dealing with data from a reaction time test.我正在处理来自反应时间测试的数据。 The data contains some basic formation on id, gender, and age and is in csv format.数据包含一些关于 id、gender 和 age 的基本格式,格式为 csv。 However, the data for the reaction task is delivered as JSON-array with the following structure: ["stimulus 1", "stimulus 2", answer chosen, reaction time].但是,反应任务的数据以 JSON 数组的形式提供,具有以下结构:[“刺激 1”、“刺激 2”、选择的答案、反应时间]。

This is an example of how the data looks like, just to give you a basic idea of it (with the exception that the JSON array is in fact much longer in the original data)这是数据外观的示例,只是为了让您了解它的基本概念(除了 JSON 数组实际上在原始数据中要长得多)

id     gender   age    reaction_task

HU3    male     34     [["prime1", "target2", 1, 1560], ["prime7", "target6", 2, 1302], ["prime4", "target5", 2, 996]]

I am quite a novice in R and looking for a method to convert this JSON-array into multiple R columns - for instance like this:我是 R 的新手,并正在寻找一种方法将此 JSON 数组转换为多个 R 列 - 例如像这样:

trial1_stimulus1     trial1_stimulus2   trial1_answer     trial1_time     trail2_stimulus1    trial2_stimulus2    etc

prime1               target2            1                 1560             prime7              target2

I found out how to separate the data from another using the following command:我发现了如何使用以下命令将数据与另一个数据分开:


df <- cbind(df, read.table(text = as.character(df$reaction_task), sep = ",", fill=TRUE) )

It worked, but turned out to be quite laborious, as I stilled had to eliminate the [] from the data manually.它有效,但结果非常费力,因为我仍然不得不手动从数据中删除[] So I was wondering wether there is a smoother way to deal with this task?所以我想知道是否有更顺畅的方法来处理这项任务?

I was trying the following code as well, but got an error message:我也在尝试以下代码,但收到错误消息:

purrr::map_dfr(sosci$A101oRAW, jsonlite::fromJSON)
Fehler: parse error: premature EOF
                                       
                     (right here) ------^

Thanks for your help!谢谢你的帮助!

Edit: Thanks a lot to Maydin for the answer provided, It works well for the example data, but when the data frame contains more than one person: I get almost the same error warning as before:编辑:非常感谢 Maydin 提供的答案,它适用于示例数据,但是当数据框包含多个人时:我收到与以前几乎相同的错误警告:

id <- c("HU3", "AB0", "IO9")
gender <- c("male", "female", "male")
age <-c(34, 87, 23)
task <- c("[[\"prime1\", \"target2\", 2, 1529], [\"prime7\", \"target6\", 2, 829], [\"prime4\", \"target5\", 1, 1872]]", "[[\"prime1\", \"target2\", 1, 1560], [\"prime7\", \"target6\", 2, 1302], [\"prime4\", \"target5\", 2, 996]]","[[\"prime1\", \"target2\", 1, 679], [\"prime7\", \"target6\", 1, 2090], [\"prime4\", \"target5\", 1, 528]]")
                                                                                                                                                                                                                                                                                                                                                                                                                 
df <- data.frame(id, gender, age, task)

library(jsonlite)
library(dplyr)

df2 <- data.frame(df[,1:3],fromJSON(as.character(df[,"task"])))
parse error: trailing garbage
          rime4", "target5", 1, 1872]] [["prime1", "target2", 1, 1560]
                     (right here) ------^
library(jsonlite)

df2 <- lapply(1:nrow(df), function(x) {

     data.frame(df[x,1:3],fromJSON(as.character(df[x,"task"])),
        row.names = NULL) })

df2 <- do.call(rbind,df2)

df2

    
   id gender age     X1      X2 X3   X4
1 HU3   male  34 prime1 target2  2 1529
2 HU3   male  34 prime7 target6  2  829
3 HU3   male  34 prime4 target5  1 1872
4 AB0 female  87 prime1 target2  1 1560
5 AB0 female  87 prime7 target6  2 1302
6 AB0 female  87 prime4 target5  2  996
7 IO9   male  23 prime1 target2  1  679
8 IO9   male  23 prime7 target6  1 2090
9 IO9   male  23 prime4 target5  1  528

I think the output above is in a nicer format, but if you like to convert this into columns,我认为上面的 output 格式更好,但是如果您想将其转换为列,

library(tidyr)

pivot_wider(data = df2, 
            id_cols = c("id","gender","age"), 
            names_from = c("X1","X2","X3","X4"), 
            values_from =c("X1","X2","X3","X4")) %>% as.data.frame()

You can change the names of the columns if you want later on by using colnames() etc.如果以后需要,可以使用colnames()等更改列的名称。

Data:数据:

df <- structure(list(id = structure(1L, .Label = "HU3", class = "factor"), 
    gender = structure(1L, .Label = "male", class = "factor"), 
    age = structure(1L, .Label = "34", class = "factor"), reaction_task = structure(1L, .Label = "[[\"prime1\", \"target2\", 1, 1560], [\"prime7\", \"target6\", 2, 1302], [\"prime4\", \"target5\", 2, 996]]", class = "factor")), class = "data.frame", row.names = c(NA, 
-1L))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM