EDITED
I have a dataframe where the identification variable contains duplicates. How can I create a new variable (VAR2) where I assign values to NA's based on this identification variable.
df <- data.frame(
ID = c(1,2,3,4,4,4,7,8,9,10),
VAR1 = c("a","b","c","d",NA,NA,"g","h","i","j")
)
The dataframe looks like this :
ID VAR1
1 a
2 b
3 c
4 d
4 NA
4 NA
7 g
8 h
9 i
10 j
The expected output
ID VAR1
1 a
2 b
3 c
4 d
4 d
4 d
7 g
8 h
9 i
10 j