# R中的排序和聚合

in那教

``````a=test\$ID
b=test\$Date
c=test\$Value
d=test\$Value1
sumA=aggregate(c, by=list(Date=b,Id=a), FUN=sum)
sumB=aggregate(d, by=list(Date=b,Id=a), FUN=sum)
final[1]=sumA[1],final[2]=sumA[2]
final[3]=sumA[3]/sumB[3]
``````

``````Date    ID    Value   Value1
1        A      20      10
1        A      25      5
1        B      50      5
1        B      50      5
1        C      25      25
1        C      35      5
2        A      30      10
2        A      25      45
2        B      40      10
2        B      40      30
``````

``````Date    ID   x
1       A    45/15=3
1       B    100/10=10
1       c    60/30=2
2       A    55/55=1
2       B    80/40=2
``````

``````Date   max- Value
1      10
2       2
``````

``````test <- structure(list(Date = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L
), ID = c("A", "A", "B", "B", "C", "C", "A", "A", "B", "B"),
Value = c(20L, 25L, 50L, 50L, 25L, 35L, 30L, 25L, 40L, 40L
), Value1 = c(10L, 5L, 5L, 5L, 25L, 5L, 10L, 45L, 10L, 30L
)), .Names = c("Date", "ID", "Value", "Value1"), class = "data.frame", row.names = c(NA,
-10L))

res1 <- aggregate(. ~ID+Date, data=test, FUN=sum)
res1 <- transform(res1, x=Value/Value1)
res1
#  ID Date Value Value1  x
#1  A    1    45     15  3
#2  B    1   100     10 10
#3  C    1    60     30  2
#4  A    2    55     55  1
#5  B    2    80     40  2

aggregate(. ~Date, data=res1[,-c(1,3:4)], FUN=max)
#   Date  x
# 1    1 10
# 2    2  2
``````
• 首先，我`aggregate`基于两个分组变量（`ID``Date) on the two value column by using`。〜`
• 创建一个新的变量`x`，即`Value/Value1``transform`
• 是否`aggregate`使用一个分组变量（`Date`）进行了最后一次运行，并删除了除以外的其余变量`x`

0 条评论