[英]Convert a string entry in a variable into multiple variables R
I have a string variable type in my data frame that has a long string (its a JSON response) with both the names of the columns I want and the values following it.我的数据框中有一个字符串变量类型,它有一个长字符串(它是一个 JSON 响应),其中包含我想要的列的名称和它后面的值。
My data frame looks like this:我的数据框如下所示:
Participant![]() |
Responses![]() |
---|---|
Emily![]() |
{"participantAge":"40","participantEducation":"Bachelors"} ![]() |
Doug![]() |
{"participantAge":"35","participantEducation":"Bachelors"} ![]() |
So for instance, the goal is to have a participantAge column with the values as the entries and participantEducation as a column with the entries因此,例如,目标是让参与者年龄列作为条目的值和参与者教育作为条目的列
Participant![]() |
Responses![]() |
participantAge![]() |
participantEducation![]() |
---|---|---|---|
Emily![]() |
{"} ![]() |
40 ![]() |
Bachelors![]() |
Doug![]() |
{"} ![]() |
35 ![]() |
Bachelors![]() |
Ive been able to do this before with python by converting the JSON response to dictionaries, but im not sure how to implement this in R.我以前可以通过将 JSON 响应转换为字典来使用 python 做到这一点,但我不确定如何在 R 中实现这一点。
You can do this as follows using dplyr
and jsonlite
您可以使用
dplyr
和jsonlite
执行以下操作
library(dplyr)
library(jsonlite)
df %>%
rowwise() %>%
mutate(Response = list(parse_json(Response))) %>%
unnest_wider(Response)
Output:输出:
Participant participantAge participantEducation
<chr> <chr> <chr>
1 Emily 35 Bachelors
2 Doug 40 Bachelors
Input:输入:
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))
You can try the jsonlite
package:你可以试试
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.