We can perform some data cleaning, split the data on comma (,
) into separate rows and different columns on colon (:
) and get the data in wide format.
library(dplyr)
library(tidyr)
df %>%
mutate(Extra = gsub('[{}]', '', Extra)) %>%
separate_rows(Extra, sep = ',') %>%
separate(Extra, c('col', 'value'), sep = ':') %>%
pivot_wider(names_from = col, values_from = value)
# Customer Group Field1 Field2 Field3
# <int> <chr> <chr> <chr> <chr>
#1 1 A1 A B C
#2 2 A2 D E F
#3 3 A3 A G D
data
df <- structure(list(Customer = 1:3, Group = c("A1", "A2", "A3"),
Extra = c("{Field1:A,Field2:B,Field3:C}",
"{Field1:D,Field2:E,Field3:F}", "{Field1:A,Field2:G,Field3:D}"
)), class = "data.frame", row.names = c(NA, -3L))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…