Admire Tarisirayi Chirume
2022-Sep-29 16:35 UTC
[R] Converting a Date variable from character to Date
Kindly request assistance to *convert a Date variable from a character to be recognized as a date*. NB: kindly take note that the data is in a csv file called *inflation*. I have included part of the file content herewith with the header for assistance. My data looks like this: *Period CPI* 2022m1 4994 2022m2 5336 2022m3 5671 2022m4 6532 2022m5 7973 2022m6 10365 2022m7 12673 2022m8 14356 2022m9 14708 I used the following command lines. class(inflation.2$cpi) inflation.2$cpi <- as.numeric(as.character(inflation.2$cpi)) *format(as.Date(inflation.2$period), "%Y-%m")* Having run the command lines above, the variable *period* in the attached CSV file remains being read as a character variable. Kindly assist. Thank you. Alternative email: addtarris at icloud.com/TChirume at rbz.co.zw Skype: admirechirume Call: +263773369884 whatsapp: +818099861504 On Thu, Sep 29, 2022 at 6:10 PM Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote:> Your attachment was stripped by the mailing list. The criteria for allowed > attachments are a bit tricky to translate into actions to apply to your > email software, so usually including part of your file in the body of the > email is the most successful approach for communicating your problem. Be > sure to use a text editor or the > > readLines("filename.csv") |> head() |> dput() > > functions in R to extract lines of your file for inclusion in the email. > > On September 29, 2022 8:52:30 AM PDT, Admire Tarisirayi Chirume < > atchirume at gmail.com> wrote: > >I kindly request for assistance to convert a Date variable from a > character > >to be recognised as a date. I used the following command lines. > > > >inflation<-read.csv("Inflation_forecasts_1.csv") > >attach(inflation) > >inflation[,1:2 ] #subsetting the dataframe > >#Renaming variables > >inflation<- rename(inflation.df, > > cpi = CPI, > > year=period) > > > >#subsetting data April 2020 to current > >inflation.2<-data.frame(inflation[-c(1:135),]) > >class(inflation.2$cpi) > >inflation.2$cpi <- as.numeric(as.character(inflation.2$cpi)) > >* format(as.Date(inflation.2$period), "%Y-%m")* > > > >Having ran the command lines above, the variable period in the attached > csv > >file remains being read as a character variable. Kindly assist. > > > >Thank you. > >______________________________________________ > >R-help at 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. > > -- > Sent from my phone. Please excuse my brevity. >[[alternative HTML version deleted]]
Try this by add a "day" to the date field library(tidyverse) library(lubridate) input <- "*Period CPI* 2022m1 4994 2022m2 5336 2022m3 5671 2022m4 6532 2022m5 7973 2022m6 10365 2022m7 12673 2022m8 14356 2022m9 14708" m_data <- read.delim(text = input, sep = "") # convert the date by adding a "day" before the conversion m_data$date <- ymd(paste0(m_data$X.Period, '-1')) m_data ## X.Period CPI. date ## 1 2022m1 4994 2022-01-01 ## 2 2022m2 5336 2022-02-01 ## 3 2022m3 5671 2022-03-01 ## 4 2022m4 6532 2022-04-01 ## 5 2022m5 7973 2022-05-01 ## 6 2022m6 10365 2022-06-01 ## 7 2022m7 12673 2022-07-01 ## 8 2022m8 14356 2022-08-01 ## 9 2022m9 14708 2022-09-01 Thanks Jim Holtman *Data Munger Guru* *What is the problem that you are trying to solve?Tell me what you want to do, not how you want to do it.* On Thu, Sep 29, 2022 at 9:36 AM Admire Tarisirayi Chirume < atchirume at gmail.com> wrote:> Kindly request assistance to *convert a Date variable from a character to > be recognized as a date*. > NB: kindly take note that the data is in a csv file called *inflation*. I > have included part of the file content herewith with the header for > assistance. > > > My data looks like this: > *Period CPI* > 2022m1 4994 > 2022m2 5336 > 2022m3 5671 > 2022m4 6532 > 2022m5 7973 > 2022m6 10365 > 2022m7 12673 > 2022m8 14356 > 2022m9 14708 > > I used the following command lines. > > > class(inflation.2$cpi) > inflation.2$cpi <- as.numeric(as.character(inflation.2$cpi)) > *format(as.Date(inflation.2$period), "%Y-%m")* > > Having run the command lines above, the variable *period* in the attached > CSV file remains being read as a character variable. Kindly assist. > > Thank you. > > > Alternative email: addtarris at icloud.com/TChirume at rbz.co.zw > Skype: admirechirume > Call: +263773369884 > whatsapp: +818099861504 > > > On Thu, Sep 29, 2022 at 6:10 PM Jeff Newmiller <jdnewmil at dcn.davis.ca.us> > wrote: > > > Your attachment was stripped by the mailing list. The criteria for > allowed > > attachments are a bit tricky to translate into actions to apply to your > > email software, so usually including part of your file in the body of the > > email is the most successful approach for communicating your problem. Be > > sure to use a text editor or the > > > > readLines("filename.csv") |> head() |> dput() > > > > functions in R to extract lines of your file for inclusion in the email. > > > > On September 29, 2022 8:52:30 AM PDT, Admire Tarisirayi Chirume < > > atchirume at gmail.com> wrote: > > >I kindly request for assistance to convert a Date variable from a > > character > > >to be recognised as a date. I used the following command lines. > > > > > >inflation<-read.csv("Inflation_forecasts_1.csv") > > >attach(inflation) > > >inflation[,1:2 ] #subsetting the dataframe > > >#Renaming variables > > >inflation<- rename(inflation.df, > > > cpi = CPI, > > > year=period) > > > > > >#subsetting data April 2020 to current > > >inflation.2<-data.frame(inflation[-c(1:135),]) > > >class(inflation.2$cpi) > > >inflation.2$cpi <- as.numeric(as.character(inflation.2$cpi)) > > >* format(as.Date(inflation.2$period), "%Y-%m")* > > > > > >Having ran the command lines above, the variable period in the attached > > csv > > >file remains being read as a character variable. Kindly assist. > > > > > >Thank you. > > >______________________________________________ > > >R-help at 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. > > > > -- > > Sent from my phone. Please excuse my brevity. > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >[[alternative HTML version deleted]]
Hello, You have to paste a day begore coercing to class "Date". A usual choice for this is day 1. inflation.2 <- 'Period CPI 2022m1 4994 2022m2 5336 2022m3 5671 2022m4 6532 2022m5 7973 2022m6 10365 2022m7 12673 2022m8 14356 2022m9 14708 ' inflation.2 <- read.table(textConnection(inflation.2), header = TRUE) inflation.2$Period2 <- as.Date(paste(inflation.2$Period, 1), "%Ym%m %d") inflation.2 #> Period CPI Period2 #> 1 2022m1 4994 2022-01-01 #> 2 2022m2 5336 2022-02-01 #> 3 2022m3 5671 2022-03-01 #> 4 2022m4 6532 2022-04-01 #> 5 2022m5 7973 2022-05-01 #> 6 2022m6 10365 2022-06-01 #> 7 2022m7 12673 2022-07-01 #> 8 2022m8 14356 2022-08-01 #> 9 2022m9 14708 2022-09-01 format(inflation.2$Period2, "%Y-%m") #> [1] "2022-01" "2022-02" "2022-03" "2022-04" "2022-05" "2022-06" "2022-07" #> [8] "2022-08" "2022-09" zoo::as.yearmon(inflation.2$Period2) #> [1] "Jan 2022" "Feb 2022" "Mar 2022" "Apr 2022" "May 2022" "Jun 2022" "Jul 2022" #> [8] "Aug 2022" "Sep 2022" Hope this helps, Rui Barradas ?s 17:35 de 29/09/2022, Admire Tarisirayi Chirume escreveu:> Kindly request assistance to *convert a Date variable from a character to > be recognized as a date*. > NB: kindly take note that the data is in a csv file called *inflation*. I > have included part of the file content herewith with the header for > assistance. > > > My data looks like this: > *Period CPI* > 2022m1 4994 > 2022m2 5336 > 2022m3 5671 > 2022m4 6532 > 2022m5 7973 > 2022m6 10365 > 2022m7 12673 > 2022m8 14356 > 2022m9 14708 > > I used the following command lines. > > > class(inflation.2$cpi) > inflation.2$cpi <- as.numeric(as.character(inflation.2$cpi)) > *format(as.Date(inflation.2$period), "%Y-%m")* > > Having run the command lines above, the variable *period* in the attached > CSV file remains being read as a character variable. Kindly assist. > > Thank you. > > > Alternative email: addtarris at icloud.com/TChirume at rbz.co.zw > Skype: admirechirume > Call: +263773369884 > whatsapp: +818099861504 > > > On Thu, Sep 29, 2022 at 6:10 PM Jeff Newmiller <jdnewmil at dcn.davis.ca.us> > wrote: > >> Your attachment was stripped by the mailing list. The criteria for allowed >> attachments are a bit tricky to translate into actions to apply to your >> email software, so usually including part of your file in the body of the >> email is the most successful approach for communicating your problem. Be >> sure to use a text editor or the >> >> readLines("filename.csv") |> head() |> dput() >> >> functions in R to extract lines of your file for inclusion in the email. >> >> On September 29, 2022 8:52:30 AM PDT, Admire Tarisirayi Chirume < >> atchirume at gmail.com> wrote: >>> I kindly request for assistance to convert a Date variable from a >> character >>> to be recognised as a date. I used the following command lines. >>> >>> inflation<-read.csv("Inflation_forecasts_1.csv") >>> attach(inflation) >>> inflation[,1:2 ] #subsetting the dataframe >>> #Renaming variables >>> inflation<- rename(inflation.df, >>> cpi = CPI, >>> year=period) >>> >>> #subsetting data April 2020 to current >>> inflation.2<-data.frame(inflation[-c(1:135),]) >>> class(inflation.2$cpi) >>> inflation.2$cpi <- as.numeric(as.character(inflation.2$cpi)) >>> * format(as.Date(inflation.2$period), "%Y-%m")* >>> >>> Having ran the command lines above, the variable period in the attached >> csv >>> file remains being read as a character variable. Kindly assist. >>> >>> Thank you. >>> ______________________________________________ >>> R-help at 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. >> >> -- >> Sent from my phone. Please excuse my brevity. >> >
@vi@e@gross m@iii@g oii gm@ii@com
2022-Sep-29 23:37 UTC
[R] Converting a Date variable from character to Date
I am not replying to the earlier request just to the part right below my message. A simple suggestion when sending people code is to add NOTHING except proper comments. Can we assume the extra asterisks are superfluous and not in your code? I mean your column is named "Period" and not "*Period" and your meaningless call to format(...) was not to *format(...)* ... And I note in R upper and lower case are not interchangeable. CPI as a column name does not match: class(inflation.2$cpi) I am not clear what the above is supposed to do. What do you want to set the class of a column in a data.frame to? If I am guessing correctly, the normal way people do a change from one TYPE to another looks more like: after <- as.character(before) In your case, your data is of type character and you want to make it a date of one kind or another. If you do a little searching, you may find a bunch of ways to convert properly formatted strings to dates or date/time types. Your data is NOT a standard date format so none of the standard ones will work. I am guessing "2022m1" may mean first month in 2022 and goes as high as 2022m12 before shifting to 2023. Good luck with that. It is far easier if your data looked like "2022-01-01" or some such format that might be read easily. You need to do one of many things I will not show here to break that date into parts or have it parsed properly as with a function like strptime() using a package. As a general comment, I hope your meaning of command line is within the R in interpreter rather than other meanings like for some shell utility. And note that generally the R method of handling a data.frame using base R or a package like dplyr requires most changes to be saved into the same or a new variable. Your sample code makes no sense to me. So assuming at some point your code got the data you want into a data.frame with a character column called inflation.1$Period, then base R would allow you to call some function that does the conversion, which I am calling doit() here) this way: inflation.1$Period <- doit(inflation.1$Period) Good Luck. You need to show a bit more knowledge of R before people can help you with more advanced tasks. -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Admire Tarisirayi Chirume Sent: Thursday, September 29, 2022 12:36 PM To: Jeff Newmiller <jdnewmil at dcn.davis.ca.us> Cc: r-help mailing list <r-help at r-project.org> Subject: [R] Converting a Date variable from character to Date Kindly request assistance to *convert a Date variable from a character to be recognized as a date*. NB: kindly take note that the data is in a csv file called *inflation*. I have included part of the file content herewith with the header for assistance. My data looks like this: *Period CPI* 2022m1 4994 2022m2 5336 2022m3 5671 2022m4 6532 2022m5 7973 2022m6 10365 2022m7 12673 2022m8 14356 2022m9 14708 I used the following command lines. class(inflation.2$cpi) inflation.2$cpi <- as.numeric(as.character(inflation.2$cpi)) *format(as.Date(inflation.2$period), "%Y-%m")* Having run the command lines above, the variable *period* in the attached CSV file remains being read as a character variable. Kindly assist. Thank you. Alternative email: addtarris at icloud.com/TChirume at rbz.co.zw Skype: admirechirume Call: +263773369884 whatsapp: +818099861504