gpt4 book ai didi

r - 在 R 中,如何根据另一列中的缺失 (NA) 值删除一列中具有重复(日期)值的行

转载 作者:行者123 更新时间:2023-12-05 09:26:05 26 4
gpt4 key购买 nike

我不知道我们如何删除具有列日期重复值的行,并选择删除具有 inst 缺失值 (NA) 的行,按 id 分组。

我的数据的一个最小工作示例是:

id <- c("N101", "N102", "N103", "N103", "N103", "N103", "N104", "N105", "N107", "N107", "N108", "N109", "N110", "N111", "N112", "N113", "N114", "N115", "N116", "N116")
inst <- c("angers", "strasbourg", NA, "angers", "montpellier", NA, "rouen", "limoges", NA, "brest", "stanne", "aphp_psl", "stanne", "strasbourg", "clairval", "stanne", "stanne", "caen", NA, "brest")
dates <- c("2008-07-13", "2008-02-13", "2008-05-13", "2008-05-13", "2010-12-14", "2011-12-19", "2013-11-12", "2014-01-31", "2008-06-13", "2009-06-09", "2009-03-10", "2008-12-10", "2010-04-15", "2008-01-13", "2017-03-13", "2014-05-14", "2012-05-15", "2009-10-22", "2010-10-18", "2011-05-03")
df1 <- data.frame (id, inst, dates)

> df1
id inst dates
1 N101 angers 2008-07-13
2 N102 strasbourg 2008-02-13
3 N103 <NA> 2008-05-13
4 N103 angers 2008-05-13
5 N103 montpellier 2010-12-14
6 N103 <NA> 2011-12-19
7 N104 rouen 2013-11-12
8 N105 limoges 2014-01-31
9 N107 <NA> 2008-06-13
10 N107 brest 2009-06-09
11 N108 stanne 2009-03-10
12 N109 aphp_psl 2008-12-10
13 N110 stanne 2010-04-15
14 N111 strasbourg 2008-01-13
15 N112 clairval 2017-03-13
16 N113 stanne 2014-05-14
17 N114 stanne 2012-05-15
18 N115 caen 2009-10-22
19 N116 <NA> 2010-10-18
20 N116 brest 2011-05-03

在上面的 MWE 中,第 3 行 3 N103 <NA> 2008-05-13应该被删除并产生 df:

id <- c("N101", "N102", "N103", "N103", "N103", "N104", "N105", "N107", "N107", "N108", "N109", "N110", "N111", "N112", "N113", "N114", "N115", "N116", "N116")
inst <- c("angers", "strasbourg", "angers", "montpellier", NA, "rouen", "limoges", NA, "brest", "stanne", "aphp_psl", "stanne", "strasbourg", "clairval", "stanne", "stanne", "caen", NA, "brest")
dates <- c("2008-07-13", "2008-02-13", "2008-05-13", "2010-12-14", "2011-12-19", "2013-11-12", "2014-01-31", "2008-06-13", "2009-06-09", "2009-03-10", "2008-12-10", "2010-04-15", "2008-01-13", "2017-03-13", "2014-05-14", "2012-05-15", "2009-10-22", "2010-10-18", "2011-05-03")
df2 <- data.frame (id, inst, dates)

> df2
id inst dates
1 N101 angers 2008-07-13
2 N102 strasbourg 2008-02-13
3 N103 angers 2008-05-13
4 N103 montpellier 2010-12-14
5 N103 <NA> 2011-12-19
6 N104 rouen 2013-11-12
7 N105 limoges 2014-01-31
8 N107 <NA> 2008-06-13
9 N107 brest 2009-06-09
10 N108 stanne 2009-03-10
11 N109 aphp_psl 2008-12-10
12 N110 stanne 2010-04-15
13 N111 strasbourg 2008-01-13
14 N112 clairval 2017-03-13
15 N113 stanne 2014-05-14
16 N114 stanne 2012-05-15
17 N115 caen 2009-10-22
18 N116 <NA> 2010-10-18
19 N116 brest 2011-05-03

有什么想法吗?

感谢您的帮助。

最佳答案

通过一些排序,让 NA 排在最后,然后检查重复,以避免组操作:

o <- order(df1$id, df1$dates, is.na(df1$inst))
df1[o,][!duplicated(df1[o, c("id","dates")]),]
# id inst dates
#1 N101 angers 2008-07-13
#2 N102 strasbourg 2008-02-13
#4 N103 angers 2008-05-13
#5 N103 montpellier 2010-12-14
#6 N103 <NA> 2011-12-19
#7 N104 rouen 2013-11-12
#8 N105 limoges 2014-01-31
#9 N107 <NA> 2008-06-13
#10 N107 brest 2009-06-09
#11 N108 stanne 2009-03-10
#12 N109 aphp_psl 2008-12-10
#13 N110 stanne 2010-04-15
#14 N111 strasbourg 2008-01-13
#15 N112 clairval 2017-03-13
#16 N113 stanne 2014-05-14
#17 N114 stanne 2012-05-15
#18 N115 caen 2009-10-22
#19 N116 <NA> 2010-10-18
#20 N116 brest 2011-05-03

如果 dplyr 是先决条件,那么您可以对其进行调整:

df1 %>%
arrange(id, dates, is.na(inst)) %>%
filter(!duplicated(select(., id, dates)))

关于r - 在 R 中,如何根据另一列中的缺失 (NA) 值删除一列中具有重复(日期)值的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74525437/

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