R-为输出数据指定所需的行顺序。 [英] R - Specifying a desired row order for the output data.frame of aggregate()
问题描述
我 aggregate()
每个站点的
R data.frame的级别如下:值
列总和
I aggregate()
the value
column sums per site
levels of the R data.frame given below:
set.seed(2013)
df <- data.frame(site = sample(c("A","B","C"), 10, replace = TRUE),
currency = sample(c("USD", "EUR", "GBP", "CNY", "CHF"),10, replace=TRUE, prob=c(10,6,5,6,0.5)),
value = sample(seq(1:10)/10,10,replace=FALSE))
df.site.sums <- aggregate(value ~ site, data=df, FUN=sum)
df.site.sums
# site value
#1 A 0.2
#2 B 0.6
#3 C 4.7
但是,我希望能够指定生成的 df.site.sums
的行顺序。例如:
However, I would like to be able to specify the row order of the resulting df.site.sums
. For instance like:
reorder <- c("C","B","A")
?special_sort(df, BY=site, ORDER=reorder) # imaginary function
# site value
#1 C 4.7
#2 B 0.6
#3 A 0.2
如何使用基数R做到这一点?需要明确的是,这本质上是数据框行排序问题,其中上下文是 aggregate()
函数(可能无关紧要)。
How can I do this using base R? Just to be clear, this is essentially a data frame row ordering question where the context is the aggregate()
function (which may or may not matter).
这是相关的,但是不能直接解决我的问题,或者我错过了解决方案的症结所在。
This is relevant but does not directly address my issue, or I am missing the crux of the solution.
更新
为了将来参考,我找到了一种解决方案,可以根据目标向量对data.frame的行进行排序在此链接。我猜它可以用作后期处理步骤。
For future reference, I found a solution to ordering a data.frame's rows with respect to a target vector on this link. I guess it can be applied as a post-processing step.
df.site.sums[match(reorder,df.site.sums$site),]
推荐答案
这可能是可能性:将站点转换为因子
并以级别
指定顺序。
This may be a possibility: convert 'site' to a factor
and specify the order in levels
.
df$site2 <- factor(df$site, levels = c("C", "B", "A"))
aggregate(value ~ site2, data = df, FUN = sum)
# site2 value
# 1 C 4.7
# 2 B 0.6
# 3 A 0.2
更新(谢谢!)。您可以使用汇总
的非公式方法:
Update following @Ananda Mahto's comment (thanks!). You can use the 'non-formula' approach of aggregate
:
reorder <- c("C", "B", "A")
with(df, aggregate(x = list(value = value),
by = list(site = factor(site, levels = reorder)),
FUN = sum))
# site value
# 1 C 4.7
# 2 B 0.6
# 3 A 0.2
或者,在公式界面中转换为因数,然后重命名转换后的站点列:
Or, converting to factor within the formula interface, and rename the converted site column:
df2 <- aggregate(value ~ factor(site, levels = c("C", "B", "A")),
data = df, FUN = sum)
df2
names(df2) <- c("site", "value")
df2
这篇关于R-为输出数据指定所需的行顺序。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!