Search code examples
rdateposixct

How do I get my imported data to not be POSIXct, but Date?


I am not too skilled in R, and I cant figure this out. I am using the Vignette of AeRobiology. After importing, my data should look like this:

## 'data.frame':    2191 obs. of  9 variables:
##  $ date    : Date, format: "2010-01-01" "2010-01-02" ...
##  $ Alnus   : num  NA NA NA NA NA NA NA NA NA NA ...
##  $ Betula  : num  NA NA NA NA NA NA NA NA NA NA ...
##  $ Taxus   : num  NA NA NA NA NA NA NA NA NA NA ...
##  $ Fraxinus: num  NA NA NA NA NA NA NA NA NA NA ...

Instead, i have POSIXct:

$ date      : POSIXct[1:365], format: "2002-01-01" "2002-01-02" "2002-01-03" "2002-01-04" ...
 $ Alnus     : num [1:365] NA NA NA NA NA NA NA NA NA NA ...
 $ Alternaria: num [1:365] NA NA NA NA NA NA NA NA NA NA ...
 $ Ambrosia  : num [1:365] NA NA NA NA NA NA NA NA NA NA ...
 $ Artemisia : num [1:365] NA NA NA NA NA NA NA NA NA NA ...

So, I need to change the POSIXct to Date. I also dont know why there is this [1:365]? The tutorial says: As you can see, the object is a data.frame, the first column is “Date” type and the rest are “num”, which means numeric. This is the format you database must have. Normally it is automatically recognized by the import function, but if not, check the functions: as.Date(), as.numeric() and as.data.frame() I have been at this for hours..How do I get it to see it as a date?

I tried as.Date(x, origin, …), but mostly I cant even find on the internet what to do to begin with. I have tried changing the original dataset, no luck


Solution

  • To change to Date format simply run:

    my_data$date <- as.Date(my_data$date)
    

    of course change name of the variable from my_data to your name.