fwarner
fwarner

Reputation: 55

Repeat a value within each ID

I have a dataset in R in long format. Each ID does not appear the same number of times (i.e. one ID might be one row, another might appear 79 rows).

e.g.

ID      V1       V2    
1       B        0    
1       A        1    
1       C        0   
2       C        0    
3       A        0    
3       C        0

I want to create a variable which, if any of the rows for a given ID have Var2 == 1, then 1 repeats for every row of that ID

e.g.

ID      V1       V2       V3    
1       B        0        1    
1       A        1        1    
1       C        0        1    
2       C        0        0    
3       A        0        0           
3       C        0        0

Upvotes: 1

Views: 1046

Answers (3)

akrun
akrun

Reputation: 887118

Another base R option is

df$V3 <- with(df,  +(ID %in% which(rowsum(V2, ID) > 0)))

Upvotes: 0

markus
markus

Reputation: 26343

In base R we can use any - and ave for the grouping.

DF$V3 <- with(DF, ave(V2, ID, FUN = function(x) any(x == 1)))
DF
#  ID V1 V2 V3
#1  1  B  0  1
#2  1  A  1  1
#3  1  C  0  1
#4  2  C  0  0
#5  3  A  0  0
#6  3  C  0  0

data

DF <- structure(list(ID = c(1L, 1L, 1L, 2L, 3L, 3L), V1 = c("B", "A", 
"C", "C", "A", "C"), V2 = c(0L, 1L, 0L, 0L, 0L, 0L)), .Names = c("ID", 
"V1", "V2"), class = "data.frame", row.names = c(NA, -6L))

Upvotes: 2

divibisan
divibisan

Reputation: 12155

Here's a tidyverse solution.

If V2 can only be 0 or 1:

library(dplyr)
df %>%
    group_by(ID) %>%
    mutate(V3 = max(V2))

If you want to check that V2 is exactly 1.

df %>%
    group_by(ID) %>%
    mutate(V3 = as.numeric(any(V2 == 1)))

Upvotes: 1

Related Questions