我的数据框架中有一个字符串变量类型,它有一个长字符串(它是一个JSON响应),其中包含我想要的列的名称和后面的值。
我的数据帧看起来像这样:
- 每一行都是参与者
- 列为各参与者列表
- 有一个带有JSON响应的字符串条目,我希望条目的开头是变量,以及":"之后是什么
参与者 | 响应 | 艾米丽 | {"participantAge"40"、"participantEducation":"Bachelors"} | Doug> | {"participantAge"35","participantEducation":"Bachelors"} |
---|
使用dplyr
和jsonlite
library(dplyr)
library(jsonlite)
df %>%
rowwise() %>%
mutate(Response = list(parse_json(Response))) %>%
unnest_wider(Response)
输出:
Participant participantAge participantEducation
<chr> <chr> <chr>
1 Emily 35 Bachelors
2 Doug 40 Bachelors
输入:
df = structure(list(Participant = c("Emily", "Doug"), Response = c("{"participantAge":"35","participantEducation":"Bachelors"}",
"{"participantAge":"40","participantEducation":"Bachelors"}"
)), class = "data.frame", row.names = c(NA, -2L))
您可以尝试jsonlite
包:
library("jsonlite")
dat_df <- data.frame(Emily='{"participantAge":"40","participantEducation":"Bachelors"}',
Doug='{"participantAge":"35","participantEducation":"Bachelors"}')
fromJSON_rec <- apply(dat_df, 2, fromJSON)
new_df <- data.frame(matrix(NA, nrow=2, ncol=3))
colnames(new_df) <- c("Participant", "participantAge", "participantEducation")
for(i in 1:length(fromJSON_rec)){
new_df[i,] <- c(names(fromJSON_rec)[i],
fromJSON_rec[[names(fromJSON_rec)[i]]][["participantAge"]],
fromJSON_rec[[names(fromJSON_rec)[i]]][["participantEducation"]])
}
> dat_df
Emily Doug
1 {"participantAge":"40","participantEducation":"Bachelors"} {"participantAge":"35","participantEducation":"Bachelors"}
> new_df
Participant participantAge participantEducation
1 Emily 40 Bachelors
2 Doug 35 Bachelors