根据r中先前行的条件创建新行

wlzqhblo  于 2023-03-15  发布在  其他
关注(0)|答案(2)|浏览(109)

我的数据集如下所示

structure(list(id = c(1, 2, 2, 2), enrollment = c(2014, 2011, 
2012, 2013), deregister = c(2016, 9999, 9999, 9999)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -4L))

我需要将该数据集转换为以下数据集:

structure(list(id = c(1, 1, 1, 2, 2, 2), enrollment = c(2014, 
2015, 2016, 2011, 2012, 2013), deregister = c(9999, 9999, 2016, 
9999, 9999, 9999)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-6L))

其理念是:如果取消注册不是9999,则通过向注册加1来向数据集添加新行,直到注册=取消注册。使用9999对新添加行的取消注册进行编码,直到注册=取消注册。
因为我有很多观察结果,所以我想创建一个没有循环的数据集。
谢谢。

0x6upsns

0x6upsns1#

可以使用mapply + :创建序列:

library(dplyr)
library(tidyr)
df %>% 
  mutate(enrollment = ifelse(deregister != 9999, mapply(`:`, enrollment, deregister), enrollment)) %>% 
  unnest_longer(enrollment) %>% 
  mutate(deregister = replace(deregister, enrollment != deregister, 9999))

#   id enrollment deregister
# 1  1       2014       9999
# 2  1       2015       9999
# 3  1       2016       2016
# 4  2       2011       9999
# 5  2       2012       9999
# 6  2       2013       9999
brccelvz

brccelvz2#

这里是一个替代方法。请检查一下这个方法是否适用于您的原始数据集:

library(tidyr)
library(dplyr)

  df %>%
    mutate(value = ifelse(deregister != 9999,
                          enrollment + 1,
                          0)) %>% 
    pivot_longer(-id, names_to = "name", values_to = "enrollment") %>% 
    group_by(id) %>% 
    arrange(enrollment, .by_group = TRUE) %>% 
    mutate(deregister = ifelse(name=="deregister" & enrollment!=9999, enrollment, 9999)) %>% 
    filter(enrollment != 9999 & enrollment != 0) %>% 
    select(-name) %>% 
    ungroup()
id enrollment deregister
  <dbl>      <dbl>      <dbl>
1     1       2014       9999
2     1       2015       9999
3     1       2016       2016
4     2       2011       9999
5     2       2012       9999
6     2       2013       9999

相关问题