Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何按月排序或订购?_R_Dplyr_Data.table - Fatal编程技术网

R 如何按月排序或订购?

R 如何按月排序或订购?,r,dplyr,data.table,R,Dplyr,Data.table,我有数据框,并根据我的要求用xtabs将输出制成表格: df1<-data.frame( Year=sample(2016:2018,100,replace = T), Month=sample(month.abb,100,replace = T), category1=sample(letters[1:6],100,replace = T), catergory2=sample(LETTERS[8:16],100,replace = T), lic=sample(c(

我有数据框,并根据我的要求用
xtabs
将输出制成表格:

df1<-data.frame(
  Year=sample(2016:2018,100,replace = T),
  Month=sample(month.abb,100,replace = T),
  category1=sample(letters[1:6],100,replace = T),
  catergory2=sample(LETTERS[8:16],100,replace = T),
  lic=sample(c("P","F","T"),100,replace = T),
  count=sample(1:1000,100,replace = T)
)
输出:

, , lic = F

     category1
Month    a    b    c    d    e    f
  Apr    0    0    0    0    0    0
  Aug  418    0    0    0    0  208
  Dec  628    0    0    0    0    0
  Feb    0    0    0  968    0  701
  Jan  388    0    0    0    0    0
  Jul  771    0    0    0    0 2514
  Jun  987  913    0  216    0  395
  Mar  454    0    0    0    0  314
  May    0 1298    0    0    0    0
  Nov  906    0  526  262    0 1417
  Oct  783    0  853  336  310  286
  Sep    0    0    0    0  928    0

, , lic = P

     category1
Month    a    b    c    d    e    f
  Apr   13    0    0    0    0    0
  Aug    0  774    0    0  416  652
  Dec    0    0    0  241  462  123
  Feb  150  857    0  169    6    1
  Jan  954    0  567    0    0    0
  Jul  481    0    0    0    0  846
  Jun    0    0    0  484    0  535
  Mar  751    0    0    0  241    0
  May    0  549   37    0    0    2
  Nov  649    0    0    0  154  692
  Oct    0    0  182    0    0    0
  Sep    0    0  585    0  493    0

, , lic = T

     category1
Month    a    b    c    d    e    f
  Apr    0    0  410    0    0    0
  Aug    0    0    0    0    0    0
  Dec    0    0  833  289  811    0
  Feb    0 1223    0  716  366  552
  Jan  555    0  802    0 1598    0
  Jul    0    0   69    0    0  696
  Jun    0    0    0    0  190    0
  Mar    0 1165    0    0    0    0
  May  979  951  676    0    0    0
  Nov  267    0   79 1951  290  530
  Oct  230   78    0  679  321    0
  Sep    0  871    0    0    0    0
输出符合我的要求,但月份顺序不正确


我能用任何包装实现同样的目标吗?或者任何最简单的方法来获得相同的数据?

我建议将
月份
作为一个有序因子:

df1$Month <- ordered(df1$Month, levels = month.abb)

xtabs(count~Month+category1+lic,data=df1)
#, , lic = F
#
#     category1
#Month    a    b    c    d    e    f
#  Jan    0    0    0    0  563    0
#  Feb    0    0    0  826    0    0
#  Mar    0    0    3  685  443  814
#  Apr    0  848    0  474    0    0
#  May  192  412 1942    0  803  545
#  Jun  593    0    0    0  520  807
#  Jul  829  745    0    0  926    0
#  Aug 1474    0  603  376    0  706
#  Sep    0    0    0  173    0    0
#  Oct    0    0  661  915  814    0
#  Nov    0  881    0    0    0    0
#  Dec    0    0    0    0    0    0
#</snip>

df1$Month希望这就是OP的目标:

library(tidyverse)
df1<-as.tibble(df1)
df1 %>% 
 arrange(Month)


     Year Month category1 catergory2 lic   count
   <int> <fct> <fct>     <fct>      <fct> <int>
 1  2016 Apr   a         N          F       745
 2  2016 Apr   b         K          F       346
 3  2016 Apr   b         O          T        61
 4  2016 Apr   a         J          T       680
 5  2018 Apr   d         O          P       308
 6  2017 Apr   e         M          F       408
 7  2016 Apr   b         P          P       474
 8  2017 Apr   b         O          P       332
 9  2016 Apr   b         P          F       321
10  2017 Apr   e         N          T       384
# ... with 90 more rows
库(tidyverse)
df1%
安排(月)
年-月类别1类别2分类计数
2016年4月1日a N F 745
2016年4月2日b K F 346
2016年4月3日第61页
2016年4月4日a J T 680
2018年4月5日第308页
2017年4月6日e M F 408
2016年4月7日第474页
2017年4月8日第332页
2016年4月9日b P F 321
2017年4月10日东北384
# ... 还有90行

df1$Month有效,是否有机会基于lic将数据提取为数据帧?
lapply(通过(df1,df1$lic,FUN=xtabs,formula=count~Month+category1),as.data.frame.array)
Supery Roland…非常感谢您快速准确的回复。不@NelsonGon…Rolan已经更新了ans,这对我很有效。我不制作任何过滤器。u r代码有过滤器
library(tidyverse)
df1<-as.tibble(df1)
df1 %>% 
 arrange(Month)


     Year Month category1 catergory2 lic   count
   <int> <fct> <fct>     <fct>      <fct> <int>
 1  2016 Apr   a         N          F       745
 2  2016 Apr   b         K          F       346
 3  2016 Apr   b         O          T        61
 4  2016 Apr   a         J          T       680
 5  2018 Apr   d         O          P       308
 6  2017 Apr   e         M          F       408
 7  2016 Apr   b         P          P       474
 8  2017 Apr   b         O          P       332
 9  2016 Apr   b         P          F       321
10  2017 Apr   e         N          T       384
# ... with 90 more rows