V1 <- c("Name", "Paul", "Name", "Sarah", NA, NA, NA, NA, "Name", "Carl", NA, NA, "Name", "Alice", "Name", "Rita")
V2 <- c("Name", "Paul", "Name", "Sarah", "Name", "Sarah", "Name", "Sarah", "Name", "Carl", "Name", "Carl", "Name", "Alice", "Name", "Rita")
df <- data.frame(V1, V2)
df
I would like V1 to look like V2. EDIT: In the original dataset, V2 doesnt exist, I created it here to give some example data.
V1 V2
1 Name Name
2 Paul Paul
3 Name Name
4 Sarah Sarah
5 <NA> Name
6 <NA> Sarah
7 <NA> Name
8 <NA> Sarah
9 Name Name
10 Carl Carl
11 <NA> Name
12 <NA> Carl
13 Name Name
14 Alice Alice
15 Name Name
16 Rita Rita
I tried the following:
#find the positions of missings in V1
m <- which(is.na(df$V1) == TRUE)
m
[1] 5 6 7 8 11 12
#go to every position and change the value depending on the field that is 2 field above the missing
for (i in m) {
df$V1[m[i]] <- df$V1[m[i]-2]
}
The output is working, but its faulty:
V1 V2
1 Name Name
2 Paul Paul
3 Name Name
4 Sarah Sarah
5 <NA> Name
6 <NA> Sarah
7 <NA> Name
8 <NA> Sarah
9 Name Name
10 Carl Carl
11 Name Name
12 Carl Carl
13 Name Name
14 Alice Alice
15 Name Name
16 Rita Rita
Why is it working for the other cells but not the first incident? Also, I'm trying to avoid for loops, so if there is a more elegant way to do it, I would love to see one!
One option involving
dplyr
andtidyr
could be: