简体   繁体   中英

Generate new column in dataframe, based on group-event in nested groups

I have a dataframe with three "main"-groups (x: 1, 2, 3), three groups within the main-groups (v: 2, 3 or 1) and some events within the main-groups (0 and 1 in y):

x <- c(1, 1, 1, 2, 2, 3, 3, 3, 3)
v <- c(2, 3, 3, 2, 2, 1, 1, 2, 2)
y <- c(0, 0, 1, 0, 0, 0, 0, 0, 1)
df <- data.frame(x, v, y)
df

> df
  x v y
1 1 2 0
2 1 3 0
3 1 3 1
4 2 2 0
5 2 2 0
6 3 1 0
7 3 1 0
8 3 2 0
9 3 2 1

For example: In group 1 (x = 1) there are two more groups (v = 2 and v = 3), event y = 1 happens in group x = 1 and v = 3.

Now i want to generate a new column z, based on the events in y: if there is any y = 1 in one group, all cases in group v in x should get a 1 for z; else NA. How can z be generated this way? df should look like:

> df
  x v y  z
1 1 2 0 NA
2 1 3 0  1
3 1 3 1  1
4 2 2 0 NA
5 2 2 0 NA
6 3 1 0  1
7 3 1 1  1
8 3 2 0 NA
9 3 2 0 NA

I am grateful for any help.

Try this:

library(dplyr)

df %>%
group_by(x, v) %>%
mutate(
z = ifelse(any(y == 1), 1, NA)
)
df %>% group_by(x, v) %>% mutate(z = if(any(y == 1)) 1 else NA)

通过分组后xy ,新列z填充有1 ',如果有任何小号1的在y并与NA的说明。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM