R:将字符串拆分为新的行和列

时间:2018-01-20 06:29:04

标签: r

输入:

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

期望的输出:

id   food_type
12    chinese
13    italian
14    spanish

我尝试了gsub / string split,但意识到进入新的行和列是另一个问题。感谢

4 个答案:

答案 0 :(得分:3)

以下是基于eval(parse(text = ))的解决方案,将字符串转换为表达式:

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

答案 1 :(得分:1)

使用strsplitsub

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

答案 2 :(得分:1)

嘿结帐这个解决方案我希望它能帮到你。

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

答案 3 :(得分:1)

我一直在寻找一种方法来更改input并使用read.table函数来获得所需的输出。最后的步骤结果是:

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