i have this data frame below. I want to make a column, where if ind_2 = 1 first, before any of the other rows, then put a 1.
row number | ind_1 | ind_2 | ind_3 |
---|---|---|---|
1 | 0 | 0 | 0 |
2 | 0 | 1 | 0 |
3 | 1 | 0 | 0 |
4 | 0 | 1 | 1 |
desired output:
row number | ind_1 | ind_2 | ind_3 | master_ind |
---|---|---|---|---|
1 | 0 | 0 | 0 | 0 |
2 | 0 | 1 | 0 | 1 |
3 | 1 | 0 | 0 | 0 |
4 | 0 | 1 | 1 | 0 |
CodePudding user response:
You could check which
row has the first value with 1 and check based on the row_number
if it needs to return a 1 or 0 like this:
library(dplyr)
df %>%
mutate(master_ind = ifelse(row_number() == min(which(ind_2 == 1)), 1, 0))
#> row_number ind_1 ind_2 ind_3 master_ind
#> 1 1 0 0 0 0
#> 2 2 0 1 0 1
#> 3 3 1 0 0 0
#> 4 4 0 1 1 0
Created on 2023-01-25 with reprex v2.0.2
CodePudding user response:
We may use base R
to do this
df1$master_ind <- ( df1[,2] == 0 &
apply(df1[-1], 1, \(x) sum(cumsum(x[-1] == 1) == 1)) > 1
)
-output
df1
rownumber ind_1 ind_2 ind_3 master_ind
1 1 0 0 0 0
2 2 0 1 0 1
3 3 1 0 0 0
4 4 0 1 1 0
data
df1 <- structure(list(rownumber = 1:4, ind_1 = c(0L, 0L, 1L, 0L), ind_2 = c(0L,
1L, 0L, 1L), ind_3 = c(0L, 0L, 0L, 1L)), row.names = c(NA, -4L
), class = "data.frame")
CodePudding user response:
You can use match
:
df1 %>%
mutate(master_ind = (rownumber == match(1, df1$ind_2)))
rownumber ind_1 ind_2 ind_3 master_ind
1 1 0 0 0 0
2 2 0 1 0 1
3 3 1 0 0 0
4 4 0 1 1 0