I have a dataset
that has some NAs
in the time
columns as shown in the sample data below. Usually when I am modifying row values of a single column I use:
df["195", "Date_Received"] = "9/1/2017"
But now I want to add values to the NA rows in the desired columns without having to write the above statement multiple times.
Please note that the columns in the original are in date format using lubridate
.
What could be a tidy
way of doing this in R
?
Dummy Data
Date = c("2021-11-01", "2021-11-04", NA, "2021-11-15", NA)
Year = c(2021, 2021, NA, 2021, NA)
Month = c(11, 11, NA, 11, NA)
Day = c(01, 04, NA, 15, NA)
Desired Output
Date Year Month Day
2021-11-01 2021 11 01
2021-11-04 2021 11 04
2021-11-12 2021 11 12
2021-11-15 2021 11 15
2021-12-02 2021 11 02
Code
library(tidyverse)
df = data.frame(Date, Year, Month, Day)
# Replacing NAs with date values....
CodePudding user response:
I'm not sure where you are getting your date values for rows 3 and 5, but you if you have a vector of the dates you want, you can just assign them directly
df = data.frame(Date=Date)
df[is.na(Date),"Date"] <- c("2021-11-12","2021-12-02")
Output:
Date
1 2021-11-01
2 2021-11-04
3 2021-11-12
4 2021-11-15
5 2021-12-02
You can then create the year/month/day columns if necessary:
df %>% mutate(Year = year(Date), Month = month(Date), day=day(Date))
Output:
Date Year Month day
1 2021-11-01 2021 11 1
2 2021-11-04 2021 11 4
3 2021-11-12 2021 11 12
4 2021-11-15 2021 11 15
5 2021-12-02 2021 12 2