-1

I have a binary data frame like the following:

factor  ID  t1  t2  t3  t4  t5  t6  t7  t8  t9  t10
x1  a1  0   0   0   1   0   1   0   0   1   1
x2  a1  1   1   1   0   0   0   0   0   0   0
x3  a1  0   0   0   0   0   0   0   1   0   1
x1  a2  1   0   0   0   1   0   1   0   0   1
x2  a2  1   1   1   1   1   1   1   0   0   1
x3  a2  0   0   1   0   0   0   1   1   1   1
x1  a3  0   0   0   0   0   0   1   1   0   0
x2  a3  0   0   1   1   1   1   0   0   0   0
x3  a3  0   1   1   1   1   0   1   1   1   1

And I want to know for each factor and each ID when the first "1" occurred (which t). So the result would look like this:

factor  ID  t
x1  a1  t4
x2  a1  t1
x3  a1  t8
x1  a2  t1
x2  a2  t1
x3  a2  t3
x1  a3  t7
x2  a3  t3
x3  a3  t2

How do I write a code to obtain this in R? Thanks

Pegi
  • 167
  • 7

1 Answers1

0

In base R

You use the apply function and check for the minimum of which(x == 1), use those values to subset the column names.

Then you cbind it to the columns you want

cbind(df[c(1:2)], t = colnames(df[-c(1:2)])[apply(df[-c(1:2)], 1, \(x) which.max(x == 1))])
  factor ID  t
1     x1 a1 t4
2     x2 a1 t1
3     x3 a1 t8
4     x1 a2 t1
5     x2 a2 t1
6     x3 a2 t3
7     x1 a3 t7
8     x2 a3 t3
9     x3 a3 t2

In dplyr

df |> 
  rowwise() |> 
  mutate(row_max = names(df[3:12])[which.max(c_across(t1:t10))], .keep = "unused")