I realise there are alternative ways to get the outcome here, but I'm trying to understand why use of rbind in the following code results in a list, rather than a data frame, despite the input of two apparently identical data frames. It presumably relates to the data frame object returned by dplyr after group_by operation, but how can this be fixed?
The aim is to remove duplicates (on the EventValue1 and EventValue2 columns) where EventCode = X, but keep duplicates for EventCode = Y.
df <- data.frame(EventID = c("1", "2", "3", "4", "5", "6", "7", "8", "9"),
EventValue1 = c("A", "A", "B", "C", "D", "E", "E", "F", "F"),
EventValue2 = c("AA", "AA", "BB", "CC", "DD", "EE", "FF", "FF", "FF"),
EventCode = c("X", "X", "X", "X", "X", "X", "X", "Y", "Y"))
# split df by event code
df.x <- subset(df, EventCode == "X")
df.y <- subset(df, EventCode == "Y")
# remove duplicates in df.x by EventValue1 and EventValue2
df.x.2 <- df.x %>%
group_by(EventValue1, EventValue2) %>%
slice(which.min(EventID))
# recombine dfs
df <- rbind(df.x.2, df.y) # this returns a list, should be a data frame
# desired outcome
# EventID EventValue1 EventValue2 EventCode
# 1 A AA X
# 3 B AA X
# 4 C AA X
# 5 D AA X
# 6 E AA X
# 7 E AA X
# 8 F FF Y
# 9 F FF Y