Casey
Casey

Reputation: 23

R: Create a flag that is 0 or 1 for a whole group if one value in that group is below a certain threshold

I have a dataset like this:

group  services
1         75
1         105
1         110
2         110
2         110
2         110

I want to create a flag where if any of the group service values fall below 100, it is flagged 0. If they are all above 100, it is flagged 1. Output would be:

group    services      flag
1         75            0
1         105           0
1         110           0
2         110           1
2         110           1
2         110           1

The dataset is thousands of groups long, so I'm not sure how to do this. I tried using dplyr as

 dataset%>%
       group_by_(group)%>%
       mutate(flag:= services>=100)

I tried searching, but couldn't find an answer as to how to get the flag to apply to a whole group for a large dataset with lots of groups.

Upvotes: 2

Views: 1906

Answers (3)

Lamia
Lamia

Reputation: 3875

You need to include an any condition in your test, to return TRUE (or 1) if any value in your group is inferior to 100.

library(dplyr)
data %>% 
group_by(group) %>% 
mutate(flag = as.numeric(!any(services<100)))

#  A tibble: 6 x 3
# Groups:   group [2]
  group services  flag
  <int>    <int> <dbl>
1     1       75     0
2     1      105     0
3     1      110     0
4     2      110     1
5     2      110     1
6     2      110     1

Upvotes: 3

akrun
akrun

Reputation: 886938

Here is an option with data.table

library(data.table)
setDT(df1)[, flag := +(all(services >= 100)), group]
df1
#   group services flag
#1:     1       75    0
#2:     1      105    0
#3:     1      110    0
#4:     2      110    1
#5:     2      110    1
#6:     2      110    1

Upvotes: 1

markus
markus

Reputation: 26343

A base R approach

transform(df1, flag = ave(services, group, FUN = function(x) all(x >= 100)))
#  group services flag
#1     1       75    0
#2     1      105    0
#3     1      110    0
#4     2      110    1
#5     2      110    1
#6     2      110    1

Upvotes: 2

Related Questions