0

Let's say I have this data frame:

df <- data.frame(
  a = c(NA,6,6,8),
  x= c(1,2,2,4),
  y = c(NA,2,NA,NA),
  z = c("apple", 2, "2", NA), 
  d = c(NA, 5, 5, 5),stringsAsFactors = FALSE)

Rows 2 and 3 are duplicates and row 3 has an NA value. I want to delete the duplicate row with the NA value so that it looks like this:

df <- data.frame(
  a = c(NA,6,8),
  x= c(1,2,4),
  y = c(NA,2,NA),
  z = c("apple", 2, NA), 
  d = c(NA, 5, 5),stringsAsFactors = FALSE)

I tried this but it doesn't work:

  
df2 <- df %>% group_by (a,x,z,d) %>% filter(y == max(y))

Any suggestions?

3 Answers 3

1
df %>%
   arrange_all() %>%
   filter(!duplicated(fill(., everything())))
   a x  y     z  d
1 NA 1 NA apple NA
2  6 2  2     2  5
3  8 4 NA  <NA>  5
Sign up to request clarification or add additional context in comments.

Comments

0
df %>% arrange(a,x,z,d) %>% distinct(a,x,z,d,.keep_all=TRUE)

   a x  y     z  d
1  6 2  2     2  5
2  8 4 NA  <NA>  5
3 NA 1 NA apple NA

1 Comment

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
0

Fill NA values with previous non-NA and select unique rows with distinct.

library(dplyr)
library(tidyr)

df %>% fill(everything()) %>% distinct()

#   a x  y     z  d
#1 NA 1 NA apple NA
#2  6 2  2     2  5
#3  8 4 NA  <NA>  5

2 Comments

Thank you! is there a way to do it without fill and distinct? Something with group_by or filter?
group_by would not work directly since NA could be in any column and not only y and they have different values. You can use df %>% fill(everything()) %>% group_by(across()) %>% slice(1L)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.