1

I have this dataset:

dataset_ex <- data.frame(album = c("Brim","Brim","Brum","Brum"),
                         track = c("Tad","Toe","Toe","Ram"),
                         stringsAsFactors = FALSE)

  album track
1  Brim   Tad
2  Brim   Toe
3  Brum   Toe
4  Brum   Ram

And one song track is redundant (since it appeared in two albums). In this case, the song Toe. And I want to eliminate one observation with that song, so I tried:

dataset_ex %>% filter(track != "Toe" && album != "Brum")

But it returns all the observations. My expected result is:

  album track
1  Brim   Tad
2  Brim   Toe
3  Brum   Ram

Any help will be greatly appreciated.

Alexis
  • 2,104
  • 2
  • 19
  • 40
  • I appreciate the two answers below, however I would like to know why a dual condition won't work here. Please, if you have a solution in that way it will help me. Thank you. – Alexis Jun 05 '21 at 02:48

3 Answers3

1

You can use distinct() on track, keeping all other columns in the dataframe.

dataset_ex <- distinct(dataset_ex, track, .keep_all = TRUE)
dataset_ex

#   album track
# 1  Brim   Tad
# 2  Brim   Toe
# 3  Brum   Ram
Zaw
  • 1,434
  • 7
  • 15
1

I think this might work for you

library(dplyr)
dataset_ex %>% distinct(track, .keep_all= TRUE)
rawr
  • 20,481
  • 4
  • 44
  • 78
1

When comparing vectors you should use single & or | and I think the condition that you are looking for is | here -

library(dplyr)

dataset_ex %>% filter(track != "Toe" | album != "Brum")

#  album track
#1  Brim   Tad
#2  Brim   Toe
#3  Brum   Ram

Keep the rows where track is not 'Toe' or album is not 'Brum'.


Another way -

This gives the row that you want to remove

dataset_ex %>% filter(track == "Toe" & album == "Brum")

#  album track
#1  Brum   Toe

Now negate the above condition to get the rows that you want to keep -

dataset_ex %>% filter(!(track == "Toe" & album == "Brum"))

#  album track
#1  Brim   Tad
#2  Brim   Toe
#3  Brum   Ram
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213