在纵向数据中达到类别后丢弃因子级别

时间:2017-09-07 08:43:53

标签: r

我有一个记录数据的数据框,纵向结构如下:

'title' => 'required|min:3',
'page_id' => 'required_without:category_id',
'category_id' => 'required_without:page_id'

我需要删除一个类别之后的级别。基本上我需要删除类别 id event 1 1 registration 2 1 inspection 3 1 inspection 4 1 deregistration 5 2 registration 6 2 inspection 7 2 deregistration 8 2 inspection 9 3 registration 10 3 deregistration 11 3 inspection 12 3 inspection 13 4 registration 14 4 inspection 15 4 inspection 16 4 deregistration 之后的event的后续级别。数据框应如下所示:

deregistration

这是生成 id event 1 1 registration 2 1 inspection 3 1 inspection 4 1 deregistration 5 2 registration 6 2 inspection 7 2 deregistration 8 3 registration 9 3 deregistration 10 4 registration 11 4 inspection 12 4 inspection 13 4 deregistration

的代码
df

2 个答案:

答案 0 :(得分:2)

这应该有效:

library(dplyr)

df %>% group_by(id) %>%
  mutate(post.deregistration = cumsum(event == "deregistration")) %>%
  mutate(drop = cumsum(post.deregistration) > 1) %>%
  filter(!drop) %>% select(-c(post.deregistration, drop))

# A tibble: 13 x 2
# Groups:   id [4]
      id          event
   <int>         <fctr>
 1     1   registration
 2     1     inspection
 3     1     inspection
 4     1 deregistration
 5     2   registration
 6     2     inspection
 7     2 deregistration
 8     3   registration
 9     3 deregistration
10     4   registration
11     4     inspection
12     4     inspection
13     4 deregistration

答案 1 :(得分:2)

使用dplyrtidyr的解决方案。 df2是最终输出。

library(dplyr)
library(tidyr)

df2 <- df %>% 
  group_by(id) %>%
  mutate(Deregistration = ifelse(event == "deregistration", 1, NA)) %>%
  fill(Deregistration, .direction = "up") %>%
  drop_na(Deregistration) %>%
  select(-Deregistration)

df2
# A tibble: 13 x 2
# Groups:   id [4]
      id          event
   <int>         <fctr>
 1     1   registration
 2     1     inspection
 3     1     inspection
 4     1 deregistration
 5     2   registration
 6     2     inspection
 7     2 deregistration
 8     3   registration
 9     3 deregistration
10     4   registration
11     4     inspection
12     4     inspection
13     4 deregistration