gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:48:13 25 4
gpt4 key购买 nike

我遇到了这个问题。

我有一个数据框(日期),其中一些文档 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")

关于如何仅删除日期中的重复元素而不是按日期删除重复的行的任何帮助?

** 更新数据
# 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 行中的重复日期?

最佳答案

尝试这个:

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

关于R 删除字符向量中的重复元素,而不是重复的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17452584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com