-3

I have a data in the following format 200101 and I want it to be in the following format 2001-01 or 2001/01

Thanks

mms
  • 365
  • 1
  • 3
  • 12
  • You are getting down votes because you do not show us what you have tried so far and where exactly you get stuck. There are links to possible solutions popping up while you type the question! – vaettchen Jul 20 '13 at 02:03
  • I need to ask another question but i can't since people have rated my questions as -ve – mms Jul 20 '13 at 11:04
  • how Can I increase my Rate so I can ask questions? – mms Jul 27 '13 at 04:58
  • @MarwahSoliman - to [up your reputation] (http://meta.stackexchange.com/questions/17204/six-simple-tips-to-get-stack-overflow-reputation-fast), and always research if your question has already been asked before you ask it... – micstr Apr 01 '15 at 13:40

1 Answers1

3

I don't deal with dates so there may well be better approaches. Your problem is you have no day. I know the zoo package can handle this but not in the format you want. I also give a regex approach but this is not a date class, just character.

As date:

library(zoo) 
as.yearmon("200101", "%Y%m") 

## > as.yearmon("200101", "%Y%m") 
## [1] "Jan 2001"

As character:

gsub("([0-9]{4})","\\1-", "200101")

## > gsub("([0-9]{4})","\\1-", "200101")
## [1] "2001-01"

## gsub("([0-9]{4})","\\1/", "200101")
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • Now I want to plot the data such that I get 2001-01 and so on. I tried PCP <- ts(pcp, frequency = 12, start = 2001) plot (PCP) but it only gives the years. Thanks – mms Jul 20 '13 at 02:46
  • 1
    I see you have a new question to address this. Please link this response to that one and if this question has been answered you can mark it as so and provide a bit more data there so it's [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Tyler Rinker Jul 20 '13 at 03:03
  • Can I ask you a question Tyler? – mms Jul 27 '13 at 06:02
  • I need to ask one more question what shall I do and my rates are low? – mms Jul 27 '13 at 08:16
  • I would ask a new question and highlight the corner case if there is one. – Tyler Rinker Jul 27 '13 at 13:29
  • @ TylerRinker-I can't ask a new question because my rates are low so what should I do in this case? – mms Jul 30 '13 at 03:09
  • You can as rates have nothing to do with the ability to ask a question. I see you keep getting downvoted and closed because you haven't read the [posting guide](http://stackoverflow.com/help) and are asking poorly formed questions. – Tyler Rinker Jul 30 '13 at 03:27
  • Well when I press ask a question I get the following message"Sorry, we are no longer accepting questions from this account". – mms Jul 30 '13 at 18:21
  • Have a look at: http://stackoverflow.com/help/question-bans – Tyler Rinker Jul 30 '13 at 19:38
  • I don't know how to improve my questions I found them logic ones, maybe my English isn't perfect but I'm trying to ask good questions can I have your email I would like to ask one last question? – mms Jul 30 '13 at 19:52