R subset with date error

2 question:
1.
I want to find max and min of date in "InvoiceDate" Column:
range(data$InvoiceDate)
and i get an error:
Error in Summary.factor(c(6839L, 6839L, 6839L, 6839L, 6839L, 6839L, 6839L, :
‘range’ not meaningful for factors
2.
when i use below function it returns nothing
data<-subset(data,InvoiceDate>= "2010-12-09")
this is error:
Warning message:
In Ops.factor(InvoiceDate, "2010-12-09") : ‘>=’ not meaningful for factors

It looks like your InvoiceDate column is a factor, not a string. How did you make the data dataframe? A common solution for this kinds of problems is to do

options(stringsAsFactors=TRUE)

as the first line in your script.
To fix the problem now you can just change the column to strings:

date$InvoiceDate = as.character(date$InvoiceDate)

Cheers
Steen

4 Likes

@stkrog probably hit the nail on the head. You could take it a step further and convert the column into a Date vector:

date[["InvoiceDate"]] <- as.character(date[["InvoiceDate"]])
date[["InvoiceDate"]] <- as.Date(date[["InvoiceDate"]])

This would let you do more comparisons and some arithmetic with the dates. Run help("Dates") to see some documentation (mostly good for the "See also" section),

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.