Isuru
Isuru

Reputation: 101

Remove the rows which are contain same entry different two columns

I have a date frame like this

Node <- c("A","A","B","B","B","C","C")
Event <- c("Reading", "Reading", "Eating", "Eating", "Watching", "Driving", "Driving")
Next_Event <- c("Reading", "Eating", "Eating", "Watching", "Sleeping", "Driving", "Playing")
df <- data.frame(Node, Event, Next_Event)
df

The expected result

Node <- c("A","B","B","C")
Event <- c("Reading", "Eating", "Watching", "Driving")
Next_Event <- c("Eating", "Watching", "Sleeping", "Playing")
df <- data.frame(Node, Event, Next_Event)
df

Upvotes: 0

Views: 30

Answers (2)

TTS
TTS

Reputation: 1928

Here's a dplyr version of the answer provided by @holzben

df2 <- df %>%
  filter(as.character(Event) != as.character(Next_Event))

df2 output:

  Node    Event Next_Event
1    A  Reading     Eating
2    B   Eating   Watching
3    B Watching   Sleeping
4    C  Driving    Playing

Upvotes: 2

holzben
holzben

Reputation: 1471

Try to filter your data:

df[as.character(df$Event) != as.character(df$Next_Event), ]

Upvotes: 3

Related Questions