I have a data frame below
df<- data.frame(a = c(1,2,3,4,5),
b = c(6,7,8,9,10),
c = c(11,12,13,14,15),
z = c("b","c","a","a","b"))
I am trying to replace row values when this row column name is equal to the value in column Z. Desired result below
a b c z
1 1 NA 11 b
2 2 7 NA c
3 NA 8 13 a
4 NA 9 14 a
5 5 NA 15 b
I thought something like the following code applied to each line
If column name is equal to Z, replace value with NA
But I can’t understand. Any help appreciated
Hooray!
source
share