Using strsplit
and sub
:
tmp <- "(12,'chinese'),(13,'italian'),(14,'spanish')"
terms <- strsplit(tmp, "(?<=\\)),(?=\\()", perl=TRUE)
df <- lapply(terms[[1]], function(x) {
id <- sub("^\\(([^,]*).*", "\\1", x)
food_type <- sub(".*,'(.*)'\\)", "\\1", x)
z <- c(id, food_type)
return(z)
})
df <- do.call(rbind.data.frame, df)
names(df) <- c("id", "food_type")
df
id food_type
1 12 chinese
2 13 italian
3 14 spanish
source
share