我有一个数据框projection.hitters由约5000个遵守情况组成:
> head(projection.hitters)
Name Positions points PAR
223 Miguel Cabrera 3B 1007.97 NA
227 Mike Trout OF 962.63 NA
160 Joey Votto 1B 863.27 NA
244 Paul Goldschmidt 1B 841.83 NA
256 Ryan Braun OF 825.67 NA
28 Andrew McCutchen OF 823.67 NA
假设我想按位置查找Positions == 1B的第n个最佳实例,并用PAR值指定该行。所有其他1B的PAR值将由其数值上下两个点之间的差来定义第n个1B的PAR == 0的点数。
例如,如果第n个1B是Paul Goldschmidt,他的PAR == 0,而Joey Votto的PAR == 21.44,即两点之间的差。
这将针对每个职位进行。
编辑:我将需要为每个位置标识一个不同的nth实例,例如,第12名最佳1B和第80名最佳OF。
怎么样 plyr
df<-read.table(header=T,text="Name Positions points PAR
Miguel Cabrera 3B 1007.97 NA
Mike Trout OF 962.63 NA
Joey Votto 1B 863.27 NA
Paul Goldschmidt 1B 841.83 NA
Ryan Braun OF 825.67 NA
Andrew McCutchen OF 823.67 NA")
n<-1 #instance
require(plyr)
ddply(df,.(Positions),mutate,PAR=abs(points-points[n]))
# Name Positions points PAR
#1 Votto 1B 863.27 0.00
#2 Goldschmidt 1B 841.83 21.44
#3 Cabrera 3B 1007.97 0.00
#4 Trout OF 962.63 0.00
#5 Braun OF 825.67 136.96
#6 McCutchen OF 823.67 138.96
每个位置n的编辑更新
n<-matrix(c(1,1,2),ncol=1,dimnames=list(unique(df$Positions))) # first, first and second instance of player per positions
require(plyr)
ddply(df,.(Positions),mutate,PAR=abs(points-points[n[Positions]]))
Name Positions points PAR
1 Votto 1B 863.27 0.00
2 Goldschmidt 1B 841.83 21.44
3 Cabrera 3B 1007.97 0.00
4 Trout OF 962.63 136.96
5 Braun OF 825.67 0.00
6 McCutchen OF 823.67 2.00
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句