Reputation: 97
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
Upvotes: 0
Views: 97
Reputation: 389135
Since your df.x.2
is grouped by EventValue1
and EventValue2
rbind
fails. It works if you ungroup
the data
library(dplyr)
rbind(df.x.2 %>% ungroup(), df.y)
# EventID EventValue1 EventValue2 EventCode
#* <fct> <fct> <fct> <fct>
#1 1 A AA X
#2 3 B BB X
#3 4 C CC X
#4 5 D DD X
#5 6 E EE X
#6 7 E FF X
#7 8 F FF Y
#8 9 F FF Y
Or use the dplyr
specific bind_rows
which will still keep the grouping
bind_rows(df.x.2, df.y)
Upvotes: 1
Reputation: 581
Use bind_rows
instead of rbind
:
df <- bind_rows(df.x.2, df.y)
df
# A tibble: 8 x 4
# Groups: EventValue1, EventValue2 [7]
EventID EventValue1 EventValue2 EventCode
<fct> <fct> <fct> <fct>
1 1 A AA X
2 3 B BB X
3 4 C CC X
4 5 D DD X
5 6 E EE X
6 7 E FF X
7 8 F FF Y
8 9 F FF Y
Upvotes: 0