How to transform a key/value string into distinct rows?

前端 未结 2 1333
不思量自难忘°
不思量自难忘° 2021-01-23 01:10

I have a R dataset with key value strings which looks like below:

quest<-data.frame(city=c(\"Atlanta\",\"New York\",\"Atlanta\",\"Tampa\"), key_value=c(\"rev=         


        
2条回答
  •  别那么骄傲
    2021-01-23 01:39

    Split by ;, then by = and |, and combine into a matrix, using the first part as the name. Then repeat the rows of the original data frame by however many rows were found for each, and combine. I don't convert here any columns to numeric, they're left as character.

    a <- strsplit(as.character(quest$key_value), ";")
    a <- lapply(a, function(x) {
        x <- do.call(cbind, strsplit(x, "[=|]"))
        colnames(x) <- x[1,]
        x[-1,,drop=FALSE]
    })
    b <- quest[rep(seq_along(a), sapply(a, nrow)), colnames(quest) != "key_value", drop=FALSE]
    out <- cbind(b, do.call(rbind, a), stringsAsFactors=FALSE)
    rownames(out) <- NULL
    out
    ##       city   rev qty   zip
    ## 1  Atlanta    63   1 45987
    ## 2 New York 10.60   1 12686
    ## 3 New York    34   2 12694
    ## 4  Atlanta    12   1 74268
    ## 5    Tampa     3   1 33684
    ## 6    Tampa    24   6 36842
    ## 7    Tampa     8   3 30254
    

提交回复
热议问题