从 R 中的字符串中提取多个引用值
Extracting Multiple Quoted Values from String in R
我正在尝试从一个字符串中提取多个引用值:
{ "What is your license plate number?": "FSD15a", "What is the color of your car?": "Grey", "What is the make AND model of your car?": "Hyundai Sonata", "What are the last three digits of your zip code?": "043", "What are the last three digits of your phone number?": "681"}
具体来说,我希望这些值中的每一个(例如,您的车牌号是多少?FSD15a,您汽车的品牌和型号是什么,现代索纳塔等)都是 df 中的新列,并且具有之前使用以下代码仅提取数字:
df %>% mutate(col1 = str_extract_all(Custom.Fields, "\d+")) %>%
unnest_wider(c(col1)) %>%
set_names(c(names(df), str_c('col', seq_along(.)[-1])))
我这辈子都想不出如何用引用的值来做到这一点。如果您能提供任何帮助,我们将不胜感激!
使用jsonlite
更容易
library(jsonlite)
as.data.frame(fromJSON(Custom.Fields), check.names = FALSE)
-输出
What is your license plate number? What is the color of your car? What is the make AND model of your car?
1 FSD15a Grey Hyundai Sonata
What are the last three digits of your zip code? What are the last three digits of your phone number?
1 043 681
数据
Custom.Fields <- '{ "What is your license plate number?": "FSD15a", "What is the color of your car?": "Grey", "What is the make AND model of your car?": "Hyundai Sonata", "What are the last three digits of your zip code?": "043", "What are the last three digits of your phone number?": "681"}
我正在尝试从一个字符串中提取多个引用值:
{ "What is your license plate number?": "FSD15a", "What is the color of your car?": "Grey", "What is the make AND model of your car?": "Hyundai Sonata", "What are the last three digits of your zip code?": "043", "What are the last three digits of your phone number?": "681"}
具体来说,我希望这些值中的每一个(例如,您的车牌号是多少?FSD15a,您汽车的品牌和型号是什么,现代索纳塔等)都是 df 中的新列,并且具有之前使用以下代码仅提取数字:
df %>% mutate(col1 = str_extract_all(Custom.Fields, "\d+")) %>%
unnest_wider(c(col1)) %>%
set_names(c(names(df), str_c('col', seq_along(.)[-1])))
我这辈子都想不出如何用引用的值来做到这一点。如果您能提供任何帮助,我们将不胜感激!
使用jsonlite
library(jsonlite)
as.data.frame(fromJSON(Custom.Fields), check.names = FALSE)
-输出
What is your license plate number? What is the color of your car? What is the make AND model of your car?
1 FSD15a Grey Hyundai Sonata
What are the last three digits of your zip code? What are the last three digits of your phone number?
1 043 681
数据
Custom.Fields <- '{ "What is your license plate number?": "FSD15a", "What is the color of your car?": "Grey", "What is the make AND model of your car?": "Hyundai Sonata", "What are the last three digits of your zip code?": "043", "What are the last three digits of your phone number?": "681"}