重新排序dcast数据帧 [英] Reordering dcast data frame
本文介绍了重新排序dcast数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可以对dcast()调用
Ex
Is it possible to reorder the columns of data frame which is a result of dcast() call E.x.
的结果重新排序数据框的列。给定数据:
Given the data:
> dput(copyOfRes)
structure(list(docName = c("doc2", "doc1", "doc1", "doc1", "doc1",
"doc1", "doc1", "doc1", "doc1", "doc1", "doc1", "doc2"), day_of_week = c(11,
11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 2)), .Names = c("docName",
"week_number"), row.names = c(NA, -12L), class = "data.frame")
所以,当我使用dcast()如下:
So, when I use dcast() as follows:
library(reshape2)
dcast(copyOfRes, docName ~ week_number, length)
结果是:
docName 2 11
1 doc1 0 10
2 doc2 1 1
我想拥有减少值 week_number $的数据框c $ c>如下所示:
I would like to have the data frame with decreasing value of week_number
as follows:
docName 11 2
1 doc1 10 0
2 doc2 1 1
我尝试过 dcast(copyOfRes,docName〜sort(week_number,decrease = TRUE),length)
,但仍然不起作用。任何建议?
I tried doing dcast(copyOfRes, docName ~ sort(week_number, decreasing= TRUE), length)
, but it still does not work. Any suggestions?
推荐答案
您可以使用 reorder
$ c> rev
You can use reorder
here with rev
dcast(copyOfRes, docName ~ reorder(week_number,rev(week_number)), length)
Using week_number as value column: use value.var to override.
docName 11 2
1 doc1 10 0
2 doc2 1 1
这篇关于重新排序dcast数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文