I would like to update one column based on 2 columns
My example dataframe contains 3 columns
df <- data.frame(n1 = c(1,2,1,2,5,6),
n2 = c("a", "a", "a", NA, "b", "c"),
n3 = c("red", "red", NA, NA, NA, NA))
df
n1 n2 n3
1 1 a red
2 2 a red
3 1 a <NA>
4 2 <NA> <NA>
5 5 b <NA>
6 6 c <NA>
I would like to add red
name to row number 3
and 4
with the condition is that if values of n1
(i.e. 1,2) match with n2
(i.e. a
), even though the fourth row (n1
not match n2
).
The main point is if n2
== a
, and values of n1
associated with a
, then values of n3
that are the same row with values of n1
should be added with red
.
My desired output
n1 n2 n3
1 1 a red
2 2 a red
3 1 a red
4 2 <NA> red
5 5 b <NA>
6 6 c <NA>
Any sugesstions for this case? I hope my explanation is clear enough. Since my data is very long, I am trying to find a good to handle it.
See Question&Answers more detail:os