R:如何从两个不同的列获得百分比变化

时间:2016-05-26 22:37:17

标签: r aggregate dplyr

我正在尝试使用R解决此问题,但我似乎无法找到正确的解决方案

这就是我的数据的外观:

Carrier Station Month   TYSeats LYSeats
AAL BSB 6   10560   10560
AAL BSB 7   10912   10912
AAL BSB 8   10560   9328
AAL BSB 9   9152    7392
AAL BSB 10  9328    9152
AAL BSB 11  8976    10384
AAL BSB 12  10208   10912
AAL CNF 6   12122   12644
AAL CNF 7   12958   13516
AAL CNF 8   10868   10138
AAL CNF 9   5434    5614
AAL CNF 10  5434    7630
AAL CNF 11  8987    9241
AAL CNF 12  12122   12958

我正在使用此代码:

aggregate((TYSeats-LYSeats)/LYSeats~Carrier+Station,data=df,FUN=mean)

我原本期望的解决方案看起来像这样(sum(TYSeats) - sum(LYSeats))超过sum(LYSeats)):

1              AAL  BSB                 0.015385  
2              AAL  CNF                -0.053191

但我得到了这个(它是平均每个月的每个操作)

1              AA     BSB                0.0270417328
2              AA     CNF               -0.0603483997

有没有办法在一个简单的行/命令中完成我需要的东西?

谢谢!

5 个答案:

答案 0 :(得分:1)

您还可以使用while(true){ def values = [] // some code omitted //java.lang:type=GarbageCollector PS Scavenge, PS MarkSweep def markSweep = new GroovyMBean(server, 'java.lang:type=GarbageCollector,name=PS MarkSweep') values.push(markSweep.CollectionCount) values.push(markSweep.CollectionTime) //in ms def scavenge = new GroovyMBean(server, 'java.lang:type=GarbageCollector,name=PS Scavenge') values.push(scavenge.CollectionCount) values.push(scavenge.CollectionTime) //in ms } 包中的ddply功能:

plyr

答案 1 :(得分:1)

一个简单而快速的data.table解决方案。

library(data.table)

setDT(df)

df[ , .(PercentChange = sum(TYSEATs -LYSeats)/sum(LYSEATs)) , by =  .(Carrier, Station) ]

答案 2 :(得分:1)

我们可以使用dplyr

library(dplyr)
df1 %>% 
   group_by(Carrier, Station) %>% 
   summarise(PercentChange = (sum(TYSeats) - sum(LYSeats))/sum(LYSeats))
# Carrier Station PercentChange
#    <chr>   <chr>         <dbl>
#1     AAL     BSB    0.01538462
#2     AAL     CNF   -0.05319134

答案 3 :(得分:0)

可能值得注意的是,如果实际上是你所追求的百分比,你应该乘以100.使用@ Psidom的代码:

ddply(df, .(Carrier, Station), summarise, 
  PerentChange = ((sum(TYSeats) - sum(LYSeats))/sum(LYSeats)*100))

  Carrier Station PerentChange
 AAL     BSB     1.538462
 AAL     CNF    -5.319134

例如,1/4是25%,但

> 1/4
[1] 0.25

答案 4 :(得分:0)

df.new <- group_by(Carrier, Station) %>%
     mutate(Max = max(TYSeats, LYSeats),
     Min = min(TYSeats, LYSeats),
     Diff.per = Max/Min -1)

您可以看到Percantage的正变化