if you’ve ever come across json data, you’ll know how messy it can be and hard to use in R
this is where the jsonlite package can really come in handy
library(tidyverse)
library(jsonlite)
messy_data <- tibble(
subj = c("subj1", "subj2"),
responses = c("{\"Q0\":\"Jenny\",\"Q1\":\"Student\",\"Q2\":\"27\",\"Q3\":\"Purple\"}",
"{\"Q0\":\"Peter\",\"Q1\":\"Doctor\",\"Q2\":\"65\",\"Q3\":\"Blue\"}")
) # this is an example of what json data may look like
messy_data
## # A tibble: 2 x 2
## subj responses
## <chr> <chr>
## 1 subj1 "{\"Q0\":\"Jenny\",\"Q1\":\"Student\",\"Q2\":\"27\",\"Q3\":\"Purple\"}"
## 2 subj2 "{\"Q0\":\"Peter\",\"Q1\":\"Doctor\",\"Q2\":\"65\",\"Q3\":\"Blue\"}"
tidy_data <- messy_data %>%
mutate(
responses = map(responses, fromJSON), # from the jsonlite package
name = map(responses, ~ unlist(.x)["Q0"]) %>% unlist(),
occupation = map(responses, ~ unlist(.x)["Q1"])%>% unlist(),
age = map(responses, ~ unlist(.x)["Q2"])%>% unlist(),
color = map(responses, ~ unlist(.x)["Q3"])%>% unlist()
) %>%
select(-responses)
tidy_data
## # A tibble: 2 x 5
## subj name occupation age color
## <chr> <chr> <chr> <chr> <chr>
## 1 subj1 Jenny Student 27 Purple
## 2 subj2 Peter Doctor 65 Blue