简体   繁体   中英

How do I add a character string containing parseable fields into columns that can be added to a dataframe

I have a dataframe. In each row of the dataframe, the last column is a character string (named data_listing ). The data_listing character string is itself a series of key:value pairs separated by commas. Here is an example of one of the strings:

> data_listing[1:2]
[1] "id:4006422,memberId:2932850,price:999,make:Chevrolet,model:Cobalt,makeYear:2009,trim:LT,mileage:142000,sellerType:For Sale By Owner,dealerOptions:null,index:2"                                                                                                                                                                                                                                                                               
[2] "id:3987513,memberId:67473,price:26799,make:Audi,model:S5,makeYear:2013,trim:Prestige,mileage:44673,sellerType:Dealership,dealerOptions:{options:{VDPcarousel:true,allowUsed:true,calculator:true,carFaxIntegration:true,featuredCarousel:true,feed:true,homepageSpotlight:0,inlineSpotlight:11,limit:-1,map:true,monsterAds:true,pop:2,priceReduced:true,refresh:7,wrap:true,chat:false,inventoryComparison:true,standardFeatured:3}},index:3"

I would like to create a column in the dataframe for each value in the data_listing string. Each column will use the key value as its name.

If I run strsplit(data_listing, ",") , then I get a list of character strings. Each list element contains a character vector "key:value" pairs.

I hesitate to write a for loop to grep each sublist element and add the values to various columns in the original dataframe, but this is the only way that I can figure out how to do this.

I have looked at transform, and tidyr::separate() , but these lend themselves to greping for a single item in the character string, not for 28 values.

How would you solve this?

I would do something like this:

data_listing <- c("id:4006422,memberId:2932850,price:999,make:Chevrolet,model:Cobalt,makeYear:2009,trim:LT,mileage:142000,sellerType:For Sale By Owner,dealerOptions:null,index:2",
                  "id:3987513,memberId:67473,price:26799,make:Audi,model:S5,makeYear:2013,trim:Prestige,mileage:44673,sellerType:Dealership,dealerOptions:{options:{VDPcarousel:true,allowUsed:true,calculator:true,carFaxIntegration:true,featuredCarousel:true,feed:true,homepageSpotlight:0,inlineSpotlight:11,limit:-1,map:true,monsterAds:true,pop:2,priceReduced:true,refresh:7,wrap:true,chat:false,inventoryComparison:true,standardFeatured:3}},index:3")

library(tidyverse)

# custom fxn for use on a single element in data_listing
parser <- function(x) {
    strsplit(x, ",", ) %>%
        unlist %>%
        as.tibble %>%
        separate(value, c("colnames", "values")) %>%
        spread(colnames, values)
}

map_dfr(data_listing, parser) # apply to each element then rbind() together

# console ...
# A tibble: 2 x 28
dealerOptions      id index      make makeYear memberId mileage  model price
<chr>   <chr> <chr>     <chr>    <chr>    <chr>   <chr>  <chr> <chr>
1          null 4006422     2 Chevrolet     2009  2932850  142000 Cobalt   999
2       options 3987513     3      Audi     2013    67473   44673     S5 26799
# ... with 19 more variables: sellerType <chr>, trim <chr>, allowUsed <chr>,
#   calculator <chr>, carFaxIntegration <chr>, chat <chr>, featuredCarousel <chr>,
#   feed <chr>, homepageSpotlight <chr>, inlineSpotlight <chr>,
#   inventoryComparison <chr>, limit <chr>, map <chr>, monsterAds <chr>, pop <chr>,
#   priceReduced <chr>, refresh <chr>, standardFeatured <chr>, wrap <chr>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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