[R] Date Time in R

Marc Schwartz marc_schwartz at me.com
Tue Jul 26 14:28:36 CEST 2016


Hi, 

That eir$date might be a factor is irrelevant. There is an as.Date() method for factors, which does the factor to character coercion internally and then calls as.Date.character() on the result.

Using the example data below:

eir <- data.frame(date = c("05-30-16", "05-30-16", "05-30-16", 
                           "05-30-16", "05-30-16", "05-30-16"))

> str(eir)
'data.frame':	6 obs. of  1 variable:
 $ date: Factor w/ 1 level "05-30-16": 1 1 1 1 1 1

> eir
      date
1 05-30-16
2 05-30-16
3 05-30-16
4 05-30-16
5 05-30-16
6 05-30-16

eir$date <- as.Date(eir$date, format = "%m-%d-%y")

> str(eir)
'data.frame':	6 obs. of  1 variable:
 $ date: Date, format: "2016-05-30" ...

> eir
        date
1 2016-05-30
2 2016-05-30
3 2016-05-30
4 2016-05-30
5 2016-05-30
6 2016-05-30

eir$days <- weekdays(eir$date)

> str(eir)
'data.frame':	6 obs. of  2 variables:
 $ date: Date, format: "2016-05-30" ...
 $ days: chr  "Monday" "Monday" "Monday" "Monday" ...

> eir
        date   days
1 2016-05-30 Monday
2 2016-05-30 Monday
3 2016-05-30 Monday
4 2016-05-30 Monday
5 2016-05-30 Monday
6 2016-05-30 Monday


I would check to be sure that you do not have any typos in your code.

Regards,

Marc Schwartz


> On Jul 26, 2016, at 6:58 AM, Shivi Bhatia <shivipmp82 at gmail.com> wrote:
> 
> Hello Again,
> 
> While i tried your solution as you suggested above it seems to be working.
> Here is the output
> temp<- dput(head(eir$date))
> c("05-30-16", "05-30-16", "05-30-16", "05-30-16", "05-30-16", "05-30-16")
> however it still shows class(eir$date) as character and hence i cannot find
> weekdays from this variable.
> 
> Sorry but i still dont understand in totality how R reads dates even though
> have tried enough.
> 
> Regards, Shivi
> 
> 
> On Tue, Jul 26, 2016 at 5:12 PM, Shivi Bhatia <shivipmp82 at gmail.com> wrote:
> 
>> Thanks Duncan for the quick response. I will check again as you suggested.
>> If that doesn't work i will share a reproducible example.
>> 
>> Thanks again!!!!
>> 
>> On Tue, Jul 26, 2016 at 4:43 PM, Duncan Murdoch <murdoch.duncan at gmail.com>
>> wrote:
>> 
>>> On 26/07/2016 7:05 AM, Shivi Bhatia wrote:
>>> 
>>>> Hi Team,
>>>> 
>>>> This scenario may have come across a number of times however i checked
>>>> nabble & SO and couldn't find a solution hence request assistance.
>>>> 
>>>> I have a date variable in my data-set eir. The class of this var was
>>>> character while i had read the file in r studio. Example of date -
>>>> 05-30-16
>>>> 
>>>> To change this i have used eir$date<- as.Date(eir$date, "%m-%d-%y"). This
>>>> converts it to a date variable. However when i check few obs
>>>> with head(eir$date) all the results are <NA>.
>>>> 
>>> 
>>> I think you don't have character data like that, because I see
>>> 
>>>> as.Date("05-30-16", "%m-%d-%y")
>>> [1] "2016-05-30"
>>> 
>>> I'd guess eir$date is really a factor, because character data is
>>> frequently changed to factor automatically.  If that's the case, this
>>> should work for the conversion:
>>> 
>>> as.Date(as.character(eir$date), "%m-%d-%y")
>>> 
>>> If that doesn't work, you'll need to post something reproducible.
>>> 
>>> Duncan Murdoch
>>> 
>>> I also need to create weekdays from this date variable but until i get
>>>> this
>>>> resolved i cant find a weekday. For weekday i have used:
>>>> eir$week<- (eir$date)
>>>> eir$week<- weekdays(as.Date(eir$week))
>>>> class(eir$week)
>>>> eir$week<- as.factor(eir$week)
>>>> head(eir$week)
>>>> 
>>>> Head of this eir$week results again as expected in <NA> but shows Levels:
>>>> Friday Monday Saturday Sunday Thursday Tuesday Wednesday
>>>> 
>>>> Not sure what i should do here. Kindly suggest.



More information about the R-help mailing list