I need to create variables with date as values from combining 3 fields or variables - a field pertaining to a "month_i"
, another separate field pertaining to a "day_i"
and a field with a value "year_i"
with i
from 0 to 9. But some of the "day_i"
value is missing. I wanted to use the beginning day or the end day of the month for such. For example,
StartingDate_0 <- paste(month_0, day_0, year_0)
And I need to do that 10 times for the "StartingDates_i"
(i = 0 to 9) and another 10 times for "EndingDates_i"
(i = 0 to 9).
A portion of my code looks like this:
DataDates <- df3[("id")] %>% left_join(df3 %>% mutate(
StartDate_0=paste(X.0.Bmonth,X.0.Bday,X.0.Byear, sep="-"),
StartDate_1=paste(X.1.Bmonth,X.1. Bday,X.1.Byear, sep="-"),
StartDate_2=paste(X.2.Bmonth,X.2. Bday,X.2.Byear, sep="-"),
StartDate_3=paste(X.3.Bmonth,X.3. Bday,X.3.Byear, sep="-"),
StartDate_4=paste(X.4.Bmonth,X.4. Bday,X.4.Byear, sep="-"),
StartDate_5=paste(X.5.Bmonth,X.5. Bday,X.5.Byear, sep="-"),
StartDate_6=paste(X.6.Bmonth,X.6. Bday,X.6.Byear, sep="-"),
StartDate_7=paste(X.7.Bmonth,X.7. Bday,X.7.Byear, sep="-"),
StartDate_8=paste(X.8.Bmonth,X.8. Bday,X.8.Byear, sep="-"),
StartDate_9=paste(X.9.Bmonth,X.9. Bday,X.9.Byear, sep="-"),
EndDate_0=paste(X.0.Emonth,X.0.Eday,X.0.Eyear, sep="-"),
EndDate_1=paste(X.1.Emonth,X.1. Eday,X.1.Eyear, sep="-"),
EndDate_2=paste(X.2.Emonth,X.2. Eday,X.2.Eyear, sep="-"),
EndDate_3=paste(X.3.Emonth,X.3. Eday,X.3.Eyear, sep="-"),
EndDate_4=paste(X.4.Emonth,X.4. Eday,X.4.Eyear, sep="-"),
EndDate_5=paste(X.5.Emonth,X.5. Eday,X.5.Eyear, sep="-"),
EndDate_6=paste(X.6.Emonth,X.6. Eday,X.6.Eyear, sep="-"),
EndDate_7=paste(X.7.Emonth,X.7. Eday,X.7.Eyear, sep="-"),
EndDate_8=paste(X.8.Emonth,X.8. Eday,X.8.Eyear, sep="-"),
EndDate_9=paste(X.9.Emonth,X.9. Eday,X.9.Eyear, sep="-")))
And a sample result gave me for the StartDate_0
of "3-NA-2009"
. How can I go about doing it as "3-1-2009"
but with as.Date
? And for Enddate_0
of "5-NA-2011"
as "5-31-2011
? I tried putting an mdy()
before the paste and then use lubridate
but it failed.
Forgive my scripting as I am really trying as a beginner R user.