dplyr返回的数据帧不会与可比较的数据帧绑定

时间:2019-10-11 11:14:55

标签: r dplyr rbind

我意识到这里有获取结果的其他方法,但是我试图理解为什么在以下代码中使用rbind会导致列表而不是数据帧,尽管输入了两个看似相同的数据帧。它大概与group_by操作后dplyr返回的数据帧对象有关,但是如何解决?

目标是删除EventCode = X的重复项(在EventValue1和EventValue2列上),但保留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



2 个答案:

答案 0 :(得分:1)

由于您的df.x.2EventValue1分组,并且EventValue2 rbind失败。如果您ungroup数据

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        

或使用特定于dplyr的{​​{1}}仍将保持分组

bind_rows

答案 1 :(得分:0)

使用bind_rows代替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