R Help I have a data.frame where I've broken out the year <dbl> and an ordered month <ord> values. But I need to recombine them so I can graph mon-year in order but when I recombine I lose the month order and the results are plotted alphabetical. Year month mon_year <dbl> <ord> 2021 Mar Mar-2021 2021 Jan Jan-2021 2021 Apr Apr-2021 So do I need to convert the months back to an integer then recombine to plot. Jeff Reichman [[alternative HTML version deleted]]
There are many ways to do this in various R packages. Search on "Plot month-year data in R" or the like and choose that which works best for you. There may be a learning curve involved, naturally. (Note: you may wish to check out zoo:yearmon and zoo:autoplot first, as these seem like they might be a good fit to your task. You decide, of course.) -- Bert On Tue, May 16, 2023 at 1:37?PM Jeff Reichman <reichmanj at sbcglobal.net> wrote:> R Help > > > > I have a data.frame where I've broken out the year <dbl> and an ordered > month <ord> values. But I need to recombine them so I can graph mon-year in > order but when I recombine I lose the month order and the results are > plotted alphabetical. > > > > Year month mon_year > > <dbl> <ord> > > 2021 Mar Mar-2021 > > 2021 Jan Jan-2021 > > 2021 Apr Apr-2021 > > > > So do I need to convert the months back to an integer then recombine to > plot. > > > > Jeff Reichman > > > [[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]]
?s 21:29 de 16/05/2023, Jeff Reichman escreveu:> R Help > > > > I have a data.frame where I've broken out the year <dbl> and an ordered > month <ord> values. But I need to recombine them so I can graph mon-year in > order but when I recombine I lose the month order and the results are > plotted alphabetical. > > > > Year month mon_year > > <dbl> <ord> > > 2021 Mar Mar-2021 > > 2021 Jan Jan-2021 > > 2021 Apr Apr-2021 > > > > So do I need to convert the months back to an integer then recombine to > plot. > > > > Jeff Reichman > > > [[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.Hello, You can use function as.yearmon in package zoo to get the correct month/year order. df1 <- data.frame(Year = c(2021, 2021, 2021), Mon = c("Mar", "Jan", "Apr")) df1$mon_year <- zoo::as.yearmon(paste(df1$Mon, df1$Year)) sort(df1$mon_year) #> [1] "Jan 2021" "Mar 2021" "Apr 2021" Hope this helps, Rui Barradas
I don't use lubridate, but that package works with Date and POSIXt types, which I do use. Just remember to include a day when converting (1st of month is typical), and use an output format to hide the day when you plot. On May 16, 2023 1:29:27 PM PDT, Jeff Reichman <reichmanj at sbcglobal.net> wrote:>R Help > > > >I have a data.frame where I've broken out the year <dbl> and an ordered >month <ord> values. But I need to recombine them so I can graph mon-year in >order but when I recombine I lose the month order and the results are >plotted alphabetical. > > > >Year month mon_year > ><dbl> <ord> > >2021 Mar Mar-2021 > >2021 Jan Jan-2021 > >2021 Apr Apr-2021 > > > >So do I need to convert the months back to an integer then recombine to >plot. > > > >Jeff Reichman > > > [[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.-- Sent from my phone. Please excuse my brevity.