Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...