用编号删除列的最后一个元素

时间:2019-02-22 14:31:20

标签: r

我的数据框有一列,其连续编号在1到20个元素之间变化(此处为1-4):

df <-
numbering col_2 col_3
 1         h     t
 2         t     h
 3         h     t
 1         t     t
 2         h     t
 1         f     h
 2         h     h
 3         h     t
 4         h     h
 1         h     t 

我需要删除通用编号的最后一行才能获得

numbering col_2 col_3
 1         h     t
 2         t     h
 1         t     t
 1         f     h
 2         h     h
 3         h     t
 1         h     t

Thx

1 个答案:

答案 0 :(得分:3)

我们可以根据发生的1创建一个goruping变量,然后slice删除最后一行,如果只有一行,则保留最后一行

library(dplyr)
df %>% 
  group_by(grp = cumsum(numbering == 1)) %>% 
  slice(if(n() == 1) 1 else -n())

或使用group_map过滤行

df %>% 
   group_by(grp = cumsum(numbering == 1)) %>% 
   group_map(~ if(nrow(.x) ==1) .x else head(.x,-1L)) %>%
   ungroup %>%
   select(-grp)

数据

df <- structure(list(numbering = c(1L, 2L, 3L, 1L, 2L, 1L, 2L, 3L, 
   4L, 1L), col_2 = c("h", "t", "h", "t", "h", "f", "h", "h", "h", 
 "h"), col_3 = c("t", "h", "t", "t", "t", "h", "h", "t", "h", 
 "t")), class = "data.frame", row.names = c(NA, -10L))