I'm guessing this has already been asked on here but I'm having difficulty finding this precise issue. I have a data frame that looks like the following:
A 1 X 9 P
A 2 NA NA NA
A 3 NA NA NA
B 1 NA NA NA
B 2 Y 8 Q
B 3 NA NA NA
C 1 NA NA NA
C 2 NA NA NA
C 3 Z 7 R
In other words, there are groups (A, B, C) that have columns with many missing values. I want to fill those missing values using the existing values in those groups. There's always at least one row in each group that has the values that need to be filled in. The desired output would look like the following:
A 1 X 9 P
A 2 X 9 P
A 3 X 9 P
B 1 Y 8 Q
B 2 Y 8 Q
B 3 Y 8 Q
C 1 Z 7 R
C 2 Z 7 R
C 3 Z 7 R
It's very similar to this person's question (Filling missing value in group) except that I want to fill missing values in many columns rather than just one. In the example here, I've included three columns in which missing values need to be filled, but the actual data frame has many more.
Thanks in advance!
You can use fill
in multiple columns and in both the directions (up and down) by group.
library(dplyr)
df %>%
group_by(V1) %>%
fill(V3:V5, .direction = 'updown') %>%
ungroup()
# V1 V2 V3 V4 V5
# <chr> <int> <chr> <int> <chr>
#1 A 1 X 9 P
#2 A 2 X 9 P
#3 A 3 X 9 P
#4 B 1 Y 8 Q
#5 B 2 Y 8 Q
#6 B 3 Y 8 Q
#7 C 1 Z 7 R
#8 C 2 Z 7 R
#9 C 3 Z 7 R
data
df <- structure(list(V1 = c("A", "A", "A", "B", "B", "B", "C", "C",
"C"), V2 = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), V3 = c("X",
NA, NA, NA, "Y", NA, NA, NA, "Z"), V4 = c(9L, NA, NA, NA, 8L,
NA, NA, NA, 7L), V5 = c("P", NA, NA, NA, "Q", NA, NA, NA, "R")),
class = "data.frame", row.names = c(NA, -9L))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With