简体   繁体   English

如何通过忽略R中的NA来提取唯一行

[英]How to extract unique rows by ignoring NA's in R

I have a dataset which contains multiple rows and multiple columns and i want to extract the unique rows by ignoring NA's from one column in few cases and including NA's in few cases. 我有一个包含多行和多列的数据集,我希望通过在少数情况下从一列中忽略NA来提取唯一行,并在少数情况下包括NA。 Please look below in detail 请仔细阅读以下内容

dataset_A dataset_A

e_id      age    fn    ln     custom_id
e1234     23     sur   bab    1344789
e1234     23     sur   bab    1344789
e1234     23     sur   bab    1617
e1234     23     sur   bab    NA
e2345     22     nav   kum    NA
e2345     22     nav   kum    52109
e2345     22     nav   kum    NA
e3456     21     ash   kuma   NA
e3456     21     ash   kuma   NA
e4567     23     anu   kot    NA

Expected_output Expected_output

e_id      age    fn    ln     custom_id
e1234     23     sur   bab    1344789
e1234     23     sur   bab    1617
e2345     22     nav   kum    52109
e3456     21     ash   kuma   NA
e4567     23     anu   kot    NA

Basically, I want to ignore rows with NA from custom_id if custom_id's are present for that e_id, whereas if the user has only NA values in a custom_id column, I want to keep 1 row and ignore other rows. 基本上,如果custom_id存在于该e_id中,我想忽略来自custom_id的NA行,而如果用户在custom_id列中只有NA值,我想保留1行并忽略其他行。

Tried: 尝试:

final_output = dataset_A[order(dataset_A$custom_id),]
final_output = final_output[!duplicated(final_output[,c(1:4)]),]

With my above piece of code, I am not able to extract a few rows from my dataset like 1617 custom_id for e_1234 e_id. 使用上面的代码,我无法从我的数据集中提取几行,如1617 custom_id for e_1234 e_id。 It would be really helpful if we are able to find the solution for the same. 如果我们能够找到相同的解决方案,那将非常有用。

We could use slice from dplyr grouping by e_id and return only 1st row if all values for custom_id are NA else return all the non-NA rows and then apply distinct to get unique rows. 我们可以用slicedplyr通过分组e_id如果只返回第一行all的值custom_idNA否则返回所有的非NA行,然后应用distinct获得独一无二的行。

library(dplyr)
df %>%
  group_by(e_id) %>%
  slice(if(all(is.na(custom_id))) 1 else which(!is.na(custom_id))) %>%
  distinct()

#   e_id    age fn    ln    custom_id
#  <fct> <int> <fct> <fct>     <int>
#1 e1234    23 sur   bab     1344789
#2 e1234    23 sur   bab        1617
#3 e2345    22 nav   kum       52109
#4 e3456    21 ash   kuma         NA
#5 e4567    23 anu   kot          NA

And maybe I have over-complicated the base R approach but one using ave would be 也许我过度复杂的基础R方法,但使用ave方法

unique(df[with(df, ave(is.na(custom_id), e_id, FUN = function(x) 
   if (all(x)) c(TRUE, rep(FALSE, length(x) - 1)) else 
               replace(rep(TRUE, length(x)), x, FALSE))), ])


#    e_id age  fn   ln custom_id
#1  e1234  23 sur  bab   1344789
#3  e1234  23 sur  bab      1617
#6  e2345  22 nav  kum     52109
#8  e3456  21 ash kuma        NA
#10 e4567  23 anu  kot        NA

If understood you correctly you can use dplyr as follows: 如果理解正确,您可以使用dplyr如下:

library(dplyr)
data %>% filter (., is.na(custom_id)==FALSE) %>% distinct(.)

If you want to keep the NANs you can add if else to the slice command 如果要保留NAN,可以将if if添加到slice命令

Book2 %>%  group_by(., e_id) %>%
  slice(., ifelse(all(is.na(custom_id)), 1 , which(!is.na(custom_id))))

Edit: Someone was faster than me so please go to the previous answer 编辑:有人比我快,所以请转到上一个答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM