如何删除每个group_by中的最后一行?

时间:2019-04-07 19:18:31

标签: r dataframe dplyr

我有一个数据框,我想删除每个名称的最后一行。谁能告诉我该怎么做?预先谢谢你。

df <- data.frame(c(500,200,100,600,300,400),c(100,50,2,70,60,90),c("A","A","A","B","C","C"))
colnames(df) <- c("income","tax","name")
#> df
  income tax name
1    500 100    A
2    200  50    A
3    100   2    A
4    600  70    B
5    300  60    C
6    400  90    C
#I have tried like this but I will lost the information about B
library(dplyr)
df$row <- rownames(df)
df2 <- df %>%
  group_by(name) %>%
  filter(row != max(row))
#ideal result should be
  income tax name
     500 100    A
     200  50    A
     600  70    B
     300  60    C

1 个答案:

答案 0 :(得分:1)

如果行数大于1,则按'名称'slice分组以删除最后一行(-n())`,否则保留完整的行

df %>% 
  group_by(name) %>% 
  slice(if(n() > 1)-n() else row_number())
# A tibble: 4 x 3
# Groups:   name [3]
#  income   tax name 
#   <dbl> <dbl> <fct>
#1    500   100 A    
#2    200    50 A    
#3    600    70 B    
#4    300    60 C    

或在|中使用OR(filter

df %>% 
   group_by(name) %>%
   filter(n() == 1 | row_number() < n())