【发布时间】:2016-08-10 20:33:37
【问题描述】:
我正在对日期字段使用 reshape 函数,想知道是否有办法更改字段在 reshape 表中列出的顺序。 我尝试在重塑之前对原始表格进行排序,但没有成功。
DQ_Summary= data.frame(RuleID=c(1,2,1,2),
ProcessorTimeStamp=as.Date(c('2016-08-04','2016-08-04','2016-08-08','2016-08-08')),
ErrorCount=c(6,8,3,4))
# RuleID ProcessorTimeStamp ErrorCount
# 1 1 2016-08-04 6
# 2 2 2016-08-04 8
# 3 1 2016-08-08 3
# 4 2 2016-08-08 4
Final_Summary = reshape(DQ_Summary,
timevar = "ProcessorTimeStamp",
idvar = c("RuleID"),
direction = "wide")
上面的代码返回一个表格,格式如下:
# RuleID ErrorCount.2016-08-04 ErrorCount.2016-08-08
# 1 1 6 3
# 2 2 8 4
我希望看到字段的顺序颠倒了重塑字段:
# RuleID ErrorCount.2016-08-08 ErrorCount.2016-08-04
# 1 1 3 6
# 2 2 4 8
这可能看起来微不足道,但这是我需要的。
【问题讨论】:
-
请在“时间戳”列中使用引号。
-
你可以在 reshape 之后重新排序列吗?
-
@rawr 你重新排序的想法抓住了我,我想出了这个:ColumnCount=ncol(Reshape_Counts) DQ_Summary = Reshape_Counts[,c(1:4,ColumnCount:5)]