[R] Casting Date to char

Ben Tupper btupper @end|ng |rom b|ge|ow@org
Wed Sep 5 22:51:50 CEST 2018


Hi,

Perhaps you wanted to convert dp$rainfall.sampdate and not dp$sampdate like this...

dp$sampdate <- as.character(dp$rainfall.sampdate)

... or even better, take charge of the conversion with format() ...

dp$sampdate <- format(dp$rainfall.sampdate, format = '%Y-%m-%d')

Cheers,
Ben

> On Sep 5, 2018, at 4:17 PM, Rich Shepard <rshepard using appl-ecosys.com> wrote:
> 
>  I've read the help for as.Date: "Date Conversion Functions to and from
> Character" but the method as.character(x ...) isn't working for me:
> 
>> str(dp)
> 'data.frame':	113569 obs. of  2 variables:
> $ rainfall.sampdate: Date, format: "2005-01-01" "2005-01-02" ...
> $ rainfall.prcp    : num  0.59 0.08 0.1 0 0 0.02 0.05 0.1 0 0.02 ...
>> dp$sampdate <- as.Character(dp$sampdate)
> Error in `$<-.data.frame`(`*tmp*`, sampdata, value = character(0)) :
>  replacement has 0 rows, data has 113569
> 
>  I don't understand the error message and want to learn what I've done
> incorrectly.
> 
> Regards,
> 
> Rich
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org

Ecological Forecasting: https://eco.bigelow.org/






	[[alternative HTML version deleted]]




More information about the R-help mailing list