简体   繁体   English

将变量中的字符串条目转换为多个变量R

[英]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:我的数据框如下所示:

  • each row is a participant每一行都是参与者
  • Participant column is the list of each participants参与者栏是每个参与者的列表
  • Responses has a string entry with a JSON response, where I would like the beginning of the entry to be the variable and what is after the ":" to be the value. Responses有一个带有 JSON 响应的字符串条目,我希望条目的开头是变量,而“:”之后的内容是值。
Participant参与者 Responses回应
Emily艾米丽 {"participantAge":"40","participantEducation":"Bachelors"} {"participantAge":"40","participantEducation":"单身汉"}
Doug道格 {"participantAge":"35","participantEducation":"Bachelors"} {"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 40 Bachelors学士
Doug道格 {"} {"} 35 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您可以使用dplyrjsonlite执行以下操作


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.

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