Dealing dataframes with conditional statements

萝らか妹 提交于 2020-05-14 08:49:08

问题


Following the previous two questions:

removing the first 3 rows of a group with conditional statement in r

Assigning NAs to rows with conditional statement in r

I'm having some troubles with my code. If Instead of deleting rows, I want to assign NAs to every event that has in their first row a Value higher than 2. So, if an event is having in its first row a Value higher than 2, I want to assign NA to that row, and to the coming two rows of that event. If the event has no more rows, just assign NAs to the rows the event has.

Here is an example, with a column of the desire output I want.

Event<- c(1,1,1,1,1,2,2,2,2,3,3,4,5,6,6,6,7,7,7,7) 
Value<- c(1,0,8,0,8,8,7,1,10,4,0,1,10,3,0,0,NA,NA,5,0) 
Desire_output<- c(1,0,8,0,8,NA, NA, NA,10,NA,NA,1,NA,NA,NA,NA,NA,NA,5,0)
AAA<- data.frame(Event, Value, Desire_output) 
     Event Value    Desire_output
1      1     1             1
2      1     0             0
3      1     8             8
4      1     0             0
5      1     8             8
6      2     8            NA
7      2     7            NA
8      2     1            NA
9      2    10            10
10     3     4            NA
11     3     0            NA
12     4     1             1
13     5    10            NA
14     6     3            NA
15     6     0            NA
16     6     0            NA
17     7    NA            NA
18     7    NA            NA
19     7     5             5
20     7     0             0

Note: If an event start with an NA, do nothing (like in event 7).

please let me know if you have an idea, and thanks in advance for your time.


回答1:


Here's a dplyr pipe to do that:

library(dplyr)
AAA %>%
  group_by(Event) %>%
  mutate(
    bad = row_number() == 1 & !is.na(Value) & Value >= 2,
    bad = bad | lag(bad, default = FALSE) | lag(bad, 2, default = FALSE),
    bad = bad | is.na(Value),
    Value2 = if_else(bad, NA_real_, Value)
  ) %>%
  ungroup()
# # A tibble: 20 x 5
#    Event Value Desire_output bad   Value2
#    <dbl> <dbl>         <dbl> <lgl>  <dbl>
#  1     1     1             1 FALSE      1
#  2     1     0             0 FALSE      0
#  3     1     8             8 FALSE      8
#  4     1     0             0 FALSE      0
#  5     1     8             8 FALSE      8
#  6     2     8            NA TRUE      NA
#  7     2     7            NA TRUE      NA
#  8     2     1            NA TRUE      NA
#  9     2    10            10 FALSE     10
# 10     3     4            NA TRUE      NA
# 11     3     0            NA TRUE      NA
# 12     4     1             1 FALSE      1
# 13     5    10            NA TRUE      NA
# 14     6     3            NA TRUE      NA
# 15     6     0            NA TRUE      NA
# 16     6     0            NA TRUE      NA
# 17     7    NA            NA TRUE      NA
# 18     7    NA            NA TRUE      NA
# 19     7     5             5 FALSE      5
# 20     7     0             0 FALSE      0

I updated the data with

AAA$Desire_output[9] <- 10

since it was inconsistent with your displayed frame (and the display made more sense).



来源:https://stackoverflow.com/questions/61467044/dealing-dataframes-with-conditional-statements

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