我有一个像这样的列的数据框
Id Comment
1 \u009cYes yes for ever for ever the boys cried in their ringing voices with softened faces on 02/14/2016
2 \u009cYes yes for ever for ever the cried in their ringing voices with softened faces on 01/14/2010
3 \u009cYes yes for ever for ever t 12/04/2003
4 \u009c for ever for ever ringing voices 07/02/2002
5 \u009c for ever for ever ringing softened faces 07/09/2001
如何使用gsub替换除ringing
和02/14/2016
最后一栏应该是这样的
Id Comment
1 ringing 02/14/2016
2 ringing 01/14/2010
3 12/04/2003
4 ringing 07/02/2002
5 ringing 07/09/2001
-----根据G.Grothendieck,Frank和Dason的评论更新了问题
答案 0 :(得分:1)
怎么样:
df <- read.table(text="Id,Comment
1,\u009cYes yes for ever for ever the boys cried in their ringing voices with softened faces on 02/14/2016
2,\u009cYes yes for ever for ever the cried in their ringing voices with softened faces on 01/14/2010
3,\u009cYes yes for ever for ever t 12/04/2003
4,\u009c for ever for ever ringing voices 07/02/2002
5,\u009c for ever for ever ringing softened faces 07/09/2001", header=T, sep=",")
df$ringing <- ''
df[grep("ringing", df$Comment), 'ringing'] <- 'ringing'
df[grep("../../..", df$Comment), 'date'] <- regmatches(df$Comment,regexpr("../../..", df$Comment))
df$res <- paste(df$ringing, df$date)
答案 1 :(得分:1)
您可以使用dplyr
,如下所示。虽然可能有更好的方法来处理正则表达式(即不需要paste
)。这假定数据已经在df
。
library(dplyr)
df %>%
mutate(Comment = paste0( ifelse(grepl('ringing', Comment), 'ringing ', ''),
gsub('^.*(\\d{2}/\\d{2}/\\d{4}).*', '\\1', Comment)))