0

I am having an issue with my mutate function. I am trying to get the difference between two dates and place that in a column for further analysis. I pulled the dates from a csv file and so they were imported as strings.

Using this code:

df%>%
    rowwise()%>%
    mutate(df,age_when_trade = ifelse(flag1 == TRUE && flag2 == TRUE,as.Date(date1,"%m/%d/%y") - as.Date(date2,"%m/%d/%y")))

I have been getting the below error

Error: impossible to replicate vector of size 13

I am using RStudio 0.98.978 and R version 3.0.2

Alex
  • 542
  • 5
  • 24
  • Testing a logical vector with == TRUE is redundant. Also looks like missing flanking quote in the 'format' argument to first `as.Date` and mangled 'format' argument to second one. I'm thinking your problems are with basic syntax rather than dplyr. – IRTFM Jul 17 '15 at 16:05
  • 3
    Please provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data so we can run the code. Right now you have an invalid string with mismatched quotes in your code. – MrFlick Jul 17 '15 at 16:05
  • I updated the string. The testing of the vector was happening because when the code would run into a blank date it would kick back an error saying "character string is not in a standard unambiguous format" – Alex Jul 17 '15 at 16:25
  • You have to tell `ifelse` what to do if the condition fails, by passing a third argument to it – konvas Jul 17 '15 at 16:28

1 Answers1

0

I was able to easily answer this by taking a different route

age<-as.Date(df$date1,"%m/%d/%y") - as.Date(df$date2,"%m/%d/%y")

df<-data.frame(df,age)
Alex
  • 542
  • 5
  • 24