last occurrence of particular value with if else statement by group

六月ゝ 毕业季﹏ 提交于 2020-01-15 09:17:28

问题


Lets say I have the following data:

library(tidyverse)
cf <- data.frame(x = c("a", "a", "a", "a", "b", "b", "b", "b", "c", "c", "c", "c", "d", "d", "e", "e"), 
            y = c("free", "with", "sus", "sus", "free", "with", "free", "sus", "sus", "with", "free", "sus", "free", "free", "with", "sus"))
> cf
   x    y
1  a free
2  a with
3  a  sus
4  a  sus
5  b free
6  b with
7  b free
8  b  sus
9  c  sus
10 c with
11 c free
12 c  sus
13 d free
14 d free
15 e with
16 e  sus

I want to obtain an indicator variable equal to 1 if the last value of y by group is equal to sus and the last value before sus was with. There can be a multiple sus before with occurs as in group a. If free occurs last first before with then its zero.

Desired output:

   x    y sus_with
1  a free        0
2  a with        0
3  a  sus        0
4  a  sus        1
5  b free        0
6  b with        0
7  b free        0
8  b  sus        0
9  c  sus        0
10 c with        0
11 c free        0
12 c  sus        0
13 d free        0
14 d free        0
15 e with        0
16 e  sus        1 

I was thinking something like this:

cf %>%
  group_by(x) %>%
  mutate(sus_with = if_else(row_number() == n() & y == "sus" & last(y == "with" & y != "free"), 1, 0))

refs:

How to find the date after the last occurrence of a certain observation in R?

How to find the last occurrence of a certain observation in grouped data in R?

any suggestions please?


回答1:


We can group_by x and check if last value of y is "sus" and the last value which is not "sus" is "with" and assign value 1 for the last row of the group.

library(dplyr)

cf %>%
  group_by(x) %>%
  mutate(sus_with = as.integer(last(y) == 'sus' & last(y[y != "sus"]) == 'with' 
                     & row_number() == n()))

#    x     y     sus_with
#   <fct> <fct>    <int>
# 1 a     free         0
# 2 a     with         0
# 3 a     sus          0
# 4 a     sus          1
# 5 b     free         0
# 6 b     with         0
# 7 b     free         0
# 8 b     sus          0
# 9 c     sus          0
#10 c     with         0
#11 c     free         0
#12 c     sus          0
#13 d     free         0
#14 d     free         0
#15 e     with         0
#16 e     sus          1


来源:https://stackoverflow.com/questions/58114243/last-occurrence-of-particular-value-with-if-else-statement-by-group

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!