Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
935 views
in Technique[技术] by (71.8m points)

r - Order dataframe by month

I have calculated the maximum counts per month in this data.frame:

counts <- data.frame(year = sort(rep(2000:2009, 12)), month = rep(month.abb,10), count = sample(1:500, 120, replace = T))

library(plyr)
count_max <- ddply(counts, .(month), summarise, max.count = max(count))


  month max.count
1    Apr       470
2    Aug       389
3    Dec       446
4    Feb       487
5    Jan       473
6    Jul       460
7    Jun       488
8    Mar       449
9    May       488
10   Nov       464
11   Oct       483
12   Sep       394

I now want to sort count_max by the month.abb vector, so that month is in the usual order Jan-Dec. This is what I tried:

count_max[match(count_max$month, month.abb),]

...but it didn't work. How can I arrange count_max$month in the order Jan-Dec?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

An alternate without conversion:

count_max[order(match(count_max$month, month.abb)), ]
#    month max.count
# 5    Jan       466
# 4    Feb       356
# 8    Mar       496
# 1    Apr       489
# 9    May       498
# 7    Jun       497
# 6    Jul       491
# 2    Aug       446
# 12   Sep       414
# 11   Oct       490
# 10   Nov       416
# 3    Dec       475         

Note that in your example, match(count...) returns the position of a given month in month.abb, which is what you want to sort by. You came real close, but instead of sorting by that vector, you subsetted by it. So, for example, August is the 2nd value in your original DF, but the 8th value in month.abb, so the match value for the 2nd value in your subset vector is 8, which means you are going to put the 8th row of your original data frame (in your case March), into the second position of your new DF, instead of ranking the 2nd row in your original DF into 8th position of the new one.

The distinction is a bit of a brain twister, but if you think it through it should make sense.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...