Creating a Date Field

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

Creating a Date Field

Phillip Heinrich
The date is imbedded in the GameID character field so I created a date vector with the following code:

ari18.test3$date <- substring(ari18.test3$GameID,4,11)  And then created a new dataframe with just the Game ID and date vectors.  The date field is a character as shown by the str() command. str(test)
'data.frame': 3 obs. of  2 variables:
 $ GameID: Factor w/ 3 levels "ARI201803290",..: 1 2 3
 $ date  : chr  "20180329" "20180330" "20180331"           GameID     date
1   ARI201803290 20180329
81  ARI201803300 20180330
165 ARI201803310 20180331
> My notes from about a week ago say that the following code will turn “date” into a date field: test$date <- as.Date(test$date,format="%Y %M %D")  date becomes a date field but the data disappears into NA   str(test)
'data.frame': 3 obs. of  2 variables:
 $ GameID: Factor w/ 3 levels "ARI201803290",..: 1 2 3
 $ date  : Date, format: NA NA NA  What am I missing here?
        [[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: Creating a Date Field

Jeff Newmiller
You are not comparing your character date data with the format string. Specifically there are no spaces in your data but there are in your format string. Any characters in the format string that are not part of % date component specifications must be found in your data.

On September 24, 2019 4:54:00 PM PDT, Phillip Heinrich <[hidden email]> wrote:

>The date is imbedded in the GameID character field so I created a date
>vector with the following code:
>
>ari18.test3$date <- substring(ari18.test3$GameID,4,11)  And then
>created a new dataframe with just the Game ID and date vectors.  The
>date field is a character as shown by the str() command. str(test)
>'data.frame': 3 obs. of  2 variables:
> $ GameID: Factor w/ 3 levels "ARI201803290",..: 1 2 3
>$ date  : chr  "20180329" "20180330" "20180331"           GameID    
>date
>1   ARI201803290 20180329
>81  ARI201803300 20180330
>165 ARI201803310 20180331
>> My notes from about a week ago say that the following code will turn
>“date” into a date field: test$date <- as.Date(test$date,format="%Y %M
>%D")  date becomes a date field but the data disappears into NA  
>str(test)
>'data.frame': 3 obs. of  2 variables:
> $ GameID: Factor w/ 3 levels "ARI201803290",..: 1 2 3
> $ date  : Date, format: NA NA NA  What am I missing here?
> [[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.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[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: Creating a Date Field

Jim Lemon-4
In reply to this post by Phillip Heinrich
Hi Phillip,
Try this:

as.Date(c("20180329","20180330","20180331"),"%Y%m%d")
[1] "2018-03-29" "2018-03-30" "2018-03-31"

Note that the format argument has to match the date format exactly.

Jim

On Wed, Sep 25, 2019 at 9:54 AM Phillip Heinrich <[hidden email]> wrote:

>
> The date is imbedded in the GameID character field so I created a date vector with the following code:
>
> ari18.test3$date <- substring(ari18.test3$GameID,4,11)  And then created a new dataframe with just the Game ID and date vectors.  The date field is a character as shown by the str() command. str(test)
> 'data.frame':   3 obs. of  2 variables:
>  $ GameID: Factor w/ 3 levels "ARI201803290",..: 1 2 3
>  $ date  : chr  "20180329" "20180330" "20180331"           GameID     date
> 1   ARI201803290 20180329
> 81  ARI201803300 20180330
> 165 ARI201803310 20180331
> > My notes from about a week ago say that the following code will turn “date” into a date field: test$date <- as.Date(test$date,format="%Y %M %D")  date becomes a date field but the data disappears into NA   str(test)
> 'data.frame':   3 obs. of  2 variables:
>  $ GameID: Factor w/ 3 levels "ARI201803290",..: 1 2 3
>  $ date  : Date, format: NA NA NA  What am I missing here?
>         [[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.

______________________________________________
[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.