R: Split row into new row and column

input:

tmp <- "(12,'chinese'),(13,'italian'),(14,'spanish')"

desired result:

id   food_type
12    chinese
13    italian
14    spanish

I tried gsub / string split, but realized that adding to a new row and column is another problem. thank

+4
source share
4 answers

Here is a solution based on eval(parse(text = ))converting a string to an expression:

x <- eval(parse(text = paste0('list(', gsub('\\(', 'c\\(', tmp), ')')))

res <- as.data.frame(do.call(rbind, x), stringsAsFactors = FALSE)
names(res) <- c('id', 'food_type')

res
#   id food_type
# 1 12   chinese
# 2 13   italian
# 3 14   spanish
+3
source

Using strsplitand 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

Demo

+1
source

Hi, this is a solution, I hope this helps you.

tmp1=gsub("\\'","",gsub("\\(","",unlist(strsplit(unlist(strsplit(tmp,",")),"\\)"))))
id=as.numeric(tmp1[seq(1,length(tmp2),2)])
fooditem=tmp1[seq(0,length(tmp2),2)]
res=data.frame(id,fooditem)

  id fooditem
1 12  chinese
2 13  italian
3 14  spanish
+1
source

I was looking for a way to change inputand use the function read.tableto get the desired result. The last steps were:

df <- lapply(strsplit(tmp, "\\(|\\)\\,?", perl = TRUE), function(x){
  x <- x[x != ""]
  read.table(text = paste0(x), sep = ",", header = FALSE, stringsAsFactors = FALSE)
})
df <- do.call(rbind, df)
names(df) <- c("id", "food_type")
# Result:
#> df
#  id food_type
#1 12   chinese
#2 13   italian
#3 14   spanish
+1
source

Source: https://habr.com/ru/post/1692448/


All Articles