Hey RStudio Community!
I'm still new to R, so forgive me if there's an easy solution. I've been searching and searching, but can't figure it out.
Given a hypothetical dataframe:
a <- c(10, NA, 30, 40, NA)
b <- c(10, 20, 30, 40, NA)
c <- c(NA, 20, 30, 40, NA)
x <- c(10, NA, 30, 40, 50)
y <- c(10, 30, NA, NA, 20)
z <- c(20, NA, 30, 40, NA)
df <- data.frame(a,b,c, x, y, z)
I want to fill the NAs -- in columns x, y and z -- with 0s.
I do not want to fill the NAs in columns a, b, and c.
Ideally, I wish I could use dplyr to select the range of rows like:
df %>%
select(x:z)
...and then somehow apply a fill so that all the NAs in each of these consecutive columns become 0s.
Said another way, I want this to apply to only columns x through z, and I want to keep columns a, b, c, unchanged in my dataframe df
.
I'd appreciate it if anyone has any thoughts on how I can approach this!