Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
163 views
Welcome To Ask or Share your Answers For Others

1 Answer

In base R, create a logical vector to subset the rows of 'df' based on the unique values of 'n1' where 'n2' is "a", then do the assignment of 'n3' corresponding to that elements with the first non-NA element from 'n3'

i1 <- with(df, n1 %in% unique(n1[n2 %in% 'a']))
df$n3[i1] <- na.omit(df$n3[i1])[1]

-output

> df
  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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...