在字符向量中删除重复元素,而不是重复行。

7

我在这个问题上遇到了困境。

我有一个数据框(dates),其中包含一些文档id和日期,存储在字符向量中:

  Doc     Dates
1 12345 c("06/01/2000","08/09/2002")
2 23456 c("07/01/2000", 09/08/2003", "07/01/2000")
3 34567 c("09/06/2004", "09/06/2004", "12/30/2006")
4 45678 c("06/01/2000","08/09/2002")

我试图移除日期中的重复元素,以获得以下结果:

  Doc     Dates
1 12345 c("06/01/2000","08/09/2002")
2 23456 c("07/01/2000", 09/08/2003")
3 34567 c("09/06/2004", "12/30/2006")
4 45678 c("06/01/2000","08/09/2002")

我已经尝试过:

R>unique(dates$dates)

但它会按日期去除重复行:

  Doc     Dates
1 12345 c("06/01/2000","08/09/2002")
2 23456 c("07/01/2000", 09/08/2003")
3 34567 c("09/06/2004", "12/30/2006")

如何仅删除Dates中的重复元素,而不是通过Dates删除重复行?

** 带有数据更新

# Match some text string (dates) from some text:

df1$dates <- as.character(strapply(df1[[2]], "((\\D\\d{1,2}(/|-)\\d{1,2}(/|-)\\d{2,4})|    ([^/]\\d{1,2}(/|-)\\d{2,4})|((JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV){1}[\\s|-]{0,2}\\d{1,4}(\\D[\\s|-]{0,}\\d{2,4}){0,}))"))

# Drop first 2 columns from dataframe
df2<-df1[ -c(1,2)]

# List data
>df2
872                     7/23/2007
873 c(" 11/4/2007", " 11/4/2007")
874   c(" 4/2/2008", " 8/2/2007")
880                    11/14/2006

> class(df2)
[1] "data.frame"

> class(df2$dates)
[1] "character"

> dput(df2)
structure(list(dates = c("NULL", "NULL", " 7/23/2007", "c(\" 11/4/2007\", \" 11/4/2007\")", 
"c(\" 4/2/2008\", \" 8/2/2007\")", "NULL", "NULL", "NULL", "NULL", 
"NULL", " 11/14/2006")), .Names = "dates", class = "data.frame", row.names = 870:880)

我的问题是如何在第873行中去除重复的日期?


1
请提供 dput(dates) 的输出。最好复制/粘贴而不是重建您的数据。 - Arun
4个回答

1

试试这个:

within(dates, Dates <- lapply(Dates, unique))

Arun - 我无法从我正在使用的系统中复制/粘贴(使得提问非常困难)。我将尝试内部解决,如果没有成功,我将创建一个数据集,可以在系统外使用并重新发布。谢谢。 - user2547308
FYI - 我解决了这个问题:在strapply周围包装一个lapply(strapply(), unique):df1$date <- as.character(lapply((strapply(df1[[2]],"((\D\d{1,2}(/|-)\d{1,2}(/|-)\d{2,4})|([^/]\d{1,2}(/|-)\d{2,4})|((JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV){1}[\s|-]{0,2}\d{1,4}(\D[\s|-]{0,}\d{2,4}){0,}))")),unique)) - user2547308
@user2547308,我建议您将其发布为答案,以便完整和更好的格式。 :-) - Ferdinand.kraft

1

我解决了一个问题,即从字符向量中删除重复值 - 使用lapply(strapply(),unique)进行包装:

df1$date <- as.character(lapply((strapply(df1[[2]], "((\\D\\d{1,2}(/|-)\\d{1,2}(/|-    )\\d{2,4})|(\\s\\d{1,2}(/|-)\\d{2,4})|((JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV){1}[\\s|-]{0,2}\\d{1,4}(\\D[\\s|-]{0,}\\d{2,4}){0,}))")),unique))

感谢你的所有帮助。

0

你可能正在寻找类似于这样的东西。

 df

     Doc                                       Dates
 1 12345                c("06/01/2000","08/09/2002")
 2 23456 c("07/01/2000", "09/08/2003", "07/01/2000")
 3 34567 c("09/06/2004", "09/06/2004", "12/30/2006")
 4 45678                c("06/01/2000","08/09/2002")

 Eval and Parse
 x <- t(sapply(df[,"Dates"],function(x){unique(eval(parse(text = x)))}))
 df$Dates <- paste(x[,1],x[,2],sep=",")

 df
      Doc                 Dates
  1 12345 06/01/2000,08/09/2002
  2 23456 07/01/2000,09/08/2003
  3 34567 09/06/2004,12/30/2006
  4 45678 06/01/2000,08/09/2002


 Same can be achieved using Regex:

 paste(unique(unlist(strsplit(gsub("c\\(|\\)","",'c("24/07/2012","22/01/2012","24/07/2012")'),","))),sep = "")

 [1] "\"24/07/2012\"" "\"22/01/2012\""

 Haven't tried on data but works

0

我会在日期中使用gsub替换掉c(),然后对于每一行,我会调用strsplit并使用,进行unique操作。

未经测试,但可能像这样: sapply(dates$dates, function(x){ new.x=gsub("c(|)","",x) new.x=strsplit(new.x, ",") unique(new.x) })


我认为Dates这一列实际上是一个列表,而不是字符串。 - Ferdinand.kraft

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接