Can we draw time series plot with only month and year in R? I don't have date variable in my dataset.
Dataset looks like this-
month year percentage
1 2012 34%
2 2012 55%
.
.
.
.
1 2013 15%
Data Science Stack Exchange is a question and answer site for Data science professionals, Machine Learning specialists, and those interested in learning more about the field. It only takes a minute to sign up.
Sign up to join this communityI would convert it to a date column using paste and as.Date setting it to the first of the month.
> df <- data.frame('month'=c(1,2,3,1,2), year=c(2012, 2012, 2012, 2013, 2013))
> df
month year
1 1 2012
2 2 2012
3 3 2012
4 1 2013
5 2 2013
> df[, 'date_col'] <- as.Date(paste(1, df$month, df$year), '%d %m %Y')
> df
month year date_col
1 1 2012 2012-01-01
2 2 2012 2012-02-01
3 3 2012 2012-03-01
4 1 2013 2013-01-01
5 2 2013 2013-02-01
EDIT: If you don't want first of the month as a place holder, you can use as.yearmon
from the zoo
package
> as.yearmon(paste(df$month, df$year), '%m %Y')
[1] "Jan 2012" "Feb 2012" "Mar 2012" "Jan 2013" "Feb 2013"
zoo
package you can use as.yearmon
. Here is an example. stackoverflow.com/questions/10446833/…
$\endgroup$