I have this dataframe:
df <- structure(list(number = 1:3, a_1 = c(1L, 4L, 7L), a_2 = c(2L,
5L, 8L), a_3 = c(3L, 6L, 9L)), class = "data.frame", row.names = c(NA,
-3L))
number a_1 a_2 a_3
1 1 1 2 3
2 2 4 5 6
3 3 7 8 9
I want to mutate a new_col
and fill it with values conditional on string match of column number
to column names.
Desired output:
number a_1 a_2 a_3 new_col
<int> <int> <int> <int> <int>
1 1 1 2 3 1
2 2 4 5 6 5
3 3 7 8 9 9
I tried with str_extract
, str_detect
... but I can't do it!