首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在纵向数据中到达某个类别后,下降因子级别

在纵向数据中到达某个类别后,下降因子级别
EN

Stack Overflow用户
提问于 2017-09-07 08:43:52
回答 1查看 27关注 0票数 0

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

代码语言:javascript
复制
 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之后的后续级别。数据框架应该如下所示:

代码语言:javascript
复制
   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的de

代码语言:javascript
复制
df = data.frame(id = rep(1:4, each = 4), 
                  event = as.factor(c("registration", "inspection", "inspection", "deregistration",
                                  "registration", "inspection", "deregistration", "inspection",
                                  "registration", "deregistration","inspection", "inspection",
                                  "registration", "inspection", "inspection", "deregistration")))
EN

回答 1

Stack Overflow用户

发布于 2017-09-07 09:07:09

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

代码语言:javascript
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46091815

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档