R - Specifying a desired row order for the output

2020-07-20 04:28发布

问题:

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

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

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.


UPDATE

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),]

回答1:

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