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=
We can use tidyverse
. With separate_rows
, split the 'key_value' by ;
and expand the rows, then separate
the column into two columns ('key', 'value' at =
, expand the rows at |
(separate_rows
), grouped by 'city', 'key', get the sequence number (row_number()
) and spread
to 'wide' format
library(tidyverse)
separate_rows(quest, key_value, sep=";") %>%
separate(key_value, into = c("key", "value"), sep="=") %>%
separate_rows(value, sep="[|]", convert = TRUE) %>%
group_by(city, key) %>%
mutate(rn = row_number()) %>%
spread(key, value) %>%
select(-rn)
# A tibble: 7 x 4
# Groups: city [3]
# city qty rev zip
#*
#1 Atlanta 1 63.0 45987
#2 Atlanta 1 12.0 74268
#3 New York 1 10.6 12686
#4 New York 2 34.0 12694
#5 Tampa 1 3.0 33684
#6 Tampa 6 24.0 36842
#7 Tampa 3 8.0 30254