sorting of month in matrix in R -
i have matrix in format:
year month freq 1 2014 april 466 2 2015 april 59535 3 2014 august 10982 4 2015 august 0 5 2014 december 35881 6 2015 december 0 7 2014 february 17 8 2015 february 24258 9 2014 january 0 10 2015 january 22785 11 2014 july 2981 12 2015 july 0 13 2014 june 1279 14 2015 june 31356 15 2014 march 289 16 2015 march 40274
i need sort months on basis of occurrence i.e jan, feb, mar... when sort gets sorted on basis of first alphabet. used this:
mat <- mat[order(mat[,1], decreasing = true), ]
and looks :
row.names april august december february january july june march may november october september 1 2015 59535 0 0 24258 22785 0 31356 40274 84211 0 0 0 2 2014 466 10982 35881 17 0 2981 1279 289 879 8911 8565 4000
can sort months on basis of occurrence in r ?
suppose df
data frame derived matrix. provide such data frame in reproducible form @ end. ensure month
, year
factors appropriate levels. note month.name
builtin variable in r used here ensure month levels appropriately sorted , have assumed year
numeric column. use levelplot
this:
df2 <- transform(df, month = factor(as.character(month), levels = month.name), year = factor(year) ) library(lattice) levelplot(freq ~ year * month, df2)
note: here df
in reproducible form:
lines <- " year month freq 1 2014 april 466 2 2015 april 59535 3 2014 august 10982 4 2015 august 0 5 2014 december 35881 6 2015 december 0 7 2014 february 17 8 2015 february 24258 9 2014 january 0 10 2015 january 22785 11 2014 july 2981 12 2015 july 0 13 2014 june 1279 14 2015 june 31356 15 2014 march 289 16 2015 march 40274 " df <- read.table(text = lines, header = true)
Comments
Post a Comment