Casting Date to char

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Casting Date to char

Rich Shepard
   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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Casting Date to char

Duncan Murdoch-2
On 05/09/2018 4:17 PM, Rich Shepard 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

This might just be a typo in your message, but as.character() is the
function you want, not as.Character().

The other typo or error above is that you are trying to convert
dp$sampdate, but the dataframe has no column with that name according to
the lines above.

Duncan Murdoch

>
>     I don't understand the error message and want to learn what I've done
> incorrectly.
>
> Regards,
>
> Rich
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Casting Date to char

Rich Shepard
In reply to this post by Rich Shepard
On Wed, 5 Sep 2018, Rich Shepard wrote:

>> dp$sampdate <- as.Character(dp$sampdate)

   This from a failed attempt: as.Character not found. The reported error was
generated by using as.character.

Rich

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Casting Date to char

Rich Shepard
In reply to this post by Duncan Murdoch-2
On Wed, 5 Sep 2018, Duncan Murdoch wrote:

> This might just be a typo in your message, but as.character() is the function
> you want, not as.Character().

Duncan,

   Yes, it is a typo.

> The other typo or error above is that you are trying to convert
> dp$sampdate, but the dataframe has no column with that name according to
> the lines above.

   I now see this.

Many thanks,

Rich

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Casting Date to char

Ben Tupper-2
In reply to this post by Rich Shepard
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 <[hidden email]> 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
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Casting Date to char

Rich Shepard
On Wed, 5 Sep 2018, Ben Tupper wrote:

> 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')

Ben,

   Yep. That's what I want and didn't know how to do. The first conversion I
missed seeing and the second reflects my infamiliarity with format().

Many thanks,

Rich

______________________________________________
[hidden email] 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.