0

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.

Ekatef
  • 1,061
  • 1
  • 9
  • 12
Ashtasora
  • 35
  • 5
  • Try `"StartingDate_0"=paste(month_0,ifelse(is.na(day_0),1,day_0),year_0)` – A. Suliman Sep 24 '18 at 09:21
  • See [dplyr - mutate: use dynamic variable names](https://stackoverflow.com/questions/26003574/dplyr-mutate-use-dynamic-variable-names) which will help you to reduce the copy-past. – A. Suliman Sep 24 '18 at 09:38
  • @A.Suliman I will look into this linked document. I will also try the ifelse function you suggested. Thank you. – Ashtasora Sep 25 '18 at 06:28
  • @A.Suliman, I hope you don't mind my asking if this line of codes will work? – Ashtasora Sep 25 '18 at 08:21
  • 1
    DatesN <- function(df, n){ " varname1 <- paste0(""StartDate_"", n), varname2 <-paste0(""EndDate_"",n)" df %>% mutate(!!varname1 := paste(X.n.Bmonth, ifelse(is.na(X.n.Bday),1,X.n.Bday), X.n.Byear, sep="-")), mutate(!!varname2 := paste(X.n.Emonth, ifelse(is.na(X.n.Eday),30,X.n.Eday), X.n.Eyear, sep="-")), } for(i in 0:3) { df3 <- DatesN(df=df3, n=i) } – Ashtasora Sep 25 '18 at 08:22

0 Answers0