I have a data frame with the following structure:
x <- data.frame("id" = c(1,1,1,2,2), "v1" = c("NB","MTA","MTA","RN","CANC"), "v2" = c(1,2,2,10,9))
What I want to do is create a new column in this dataframe which is based on the values of the "v1" column. The column should check for each unique id whether the "v1" column has a "NB" or a "RN" value. If it does, the column should have that value. The result should be:
> result <- data.frame("id" = c(1,1,1,2,2), "v1" = c("NB","MTA","MTA","RN","CANC"), "v2" = c(1,2,2,10,9), "v3" = c("NB","NB","NB","RN","RN"))
> result
id v1 v2 v3
1 1 NB 1 NB
2 1 MTA 2 NB
3 1 MTA 2 NB
4 2 RN 10 RN
5 2 CANC 9 RN
I've been messing around with group-by in dplyr but cna't get it to work