删除字符后但存在相同字符的所有内容

时间:2018-10-05 10:37:22

标签: r

在这样的数据框中:

df <- data.frame(id=c(1,2,3,4), text = c("word1","word2","word3","word4"), ts = c("something1,here,other","something2,here,other","something5,here,other","something4,here,other")

我只想保留ts列中第一个逗号之前的内容,并删除之后的内容。像这样的结果:

df <- data.frame(id=c(1,2,3,4), text = c("word1","word2","word3","word4"), ts = c("something1","something2","something5","something4")

我试过了,但是不适合:

df$ts <- gsub(",","",df$ts)

我该如何进行?

1 个答案:

答案 0 :(得分:0)

你非常亲密... 使用,.*作为模式,替换逗号,并替换第一个逗号之后的所有内容(即.*部分)。

df$ts <- gsub( ",.*", "", df$ts )

#   id  text         ts
# 1  1 word1 something1
# 2  2 word2 something2
# 3  3 word3 something5
# 4  4 word4 something4            

详细了解正则表达式:http://stat545.com/block022_regular-expression.html