[R] Check Dates
David Winsemius
dwinsemius at comcast.net
Sat Jul 10 17:30:38 CEST 2010
On Jul 10, 2010, at 9:42 AM, nvanzuydam at gmail.com wrote:
> Dear Kevin,
>
> Assuming that closure and referral are two columns in your df and
> you have
> already converted them to date columns.
>
> The following should work:
>
> df$Dec04<- ifelse(df$referral <"2005-01-01" & df$closure >
> "2003-12-31" |
> df$closure== "", "Yes", "No")
>
> if you are trying to account for the column closure containing NA
> values
> then:
>
> df$Dec04<- ifelse(df$referral <"2005-01-01" & df$closure >
> "2003-12-31" |
> is.na(df$closure)== "TRUE", "Yes", "No")
The entire point of is.na() is to return a logical value, so is,na()
needs no further testing with "== 'TRUE'". It's also not clear why one
would want to consider missing data to be considered in the same
category as dates actually between the fences being constructed. Seems
better to let the value of the ifelse conditional expression be NA and
have ifelse() therefore return NA.
>
> not sure if how you have read your data in whether you included
> blanks in
> your NA strings or not?
That should have been taken care of with the as.Date step:
> as.Date("", "%d/%m/%Y")
[1] NA
> as.Date(" ", "%d/%m/%Y")
[1] NA
>
> Hope this helps,
> Natalie
>
> On Jul 10, 2010 2:29pm, "Wang, Kevin (SYD)" <kevinwang at kpmg.com.au>
> wrote:
>> I'm trying to check a column of dates and creating indicator
>> variables
>> showing different time periods.
>
>> For example, if I were to do this in Excel I would have something
>> like:
>
>> Year to Dec 2004=IF(AND(referral dateDATE(2003,12,31), LEN($closure
>> date)
>> = 0)), "Yes", "No")
>
>> for the "Year to Dec 2004" column.
>
>> I've read the data into R and converted the two columns (referral
>> date
>> and closure date) to as.Date() format (found this format from r-
>> help):
>
>> referral
>> closure
>
>> But then have trouble doing the "if" checking. I tried something
>> like:
>
>> Dec04
>> (closure > as.Date("31/12/2003", "%d/%m/%Y") || closure
>> = ""), "Yes", "No")
>
>> but got an error message:
>
>> Error in closure > as.Date("31/12/2003", "%d/%m/%Y") || closure =
>> "" :
>
>> could not find function "||
>
>
>> I haven't really played with dates in R before so would really
>> appreciate
>> some guidance on this.
>
>> (I'm avoiding Excel because there are over 130,000 rows to check,
>> and I
>> need to repeat this process for each quarter since Dec 2004. When I
>> tried
>> this in Excel it just took all the memory I have on my computer and
>> freezes the entire workbook).
>
>
--
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list