这是我在这里提出的问题的扩展: Getting Factor Means into the dataset after calculation
现在我已基本规范了我感兴趣的所有统计数据 我想搜索与这些数据集相交的人的数据集。因此,我正在搜索这样的数据集:
base3[((base3$ScaledAVG>2)&(base3$ScaledOBP>2)&(base3$ScaledK.AB<.20)),]
寻找具有所有这三个方面的玩家,但是当我运行它时,它会将Scaled K.AB值重置为.5,1或2,然后不使用该参数进行搜索。以这种方式搜索数据集是否有问题,或者是否有更好的方法以同样的方式在数据集中查找人员?
以下是一些示例数据,但它与我出版的4000条记录时没有相同的问题:
AVG = c(.350,.400,.320,.220,.100,.250,.400,.450)
Conf = c("SEC","ACC","SEC","B12","P12","ACC","B12","P12")
OBP = c(.360,.420,.360,.260,.160,.260,.460,.410)
K.AB = c(.11,.10,.09,.25,.20,.19,.05,.09)
Conf=as.factor(Conf)
d<- data.frame(Conf, AVG,OBP,K.AB)
dd <- do.call(rbind, by(d, d$Conf, FUN=function(x) { x$Scaled <- scale(x$AVG); x}))
dd <- do.call(rbind, by(d, d$Conf, FUN=function(x) { x$Scaled <- scale(x$OBP); x}))
dd <- do.call(rbind, by(d, d$Conf, FUN=function(x) { x$Scaled <- scale(x$K.AB); x}))
dd[((dd$ScaledAVG>2)&(dd$ScaledOBP>2)&(dd$ScaledK.AB<.20)),]
谢谢!
答案 0 :(得分:0)
您可能希望放弃do.call(rbind,by(...))策略,转而采用直接scale
策略。 scale function has a
data.frame`方法。
> dd <- scale(d[ ,c("AVG", "OBP", "K.AB")])
> dd
AVG OBP K.AB
[1,] 0.33566727 0.2348519 -0.3608439
[2,] 0.76878633 0.8281619 -0.5051815
[3,] 0.07579584 0.2348519 -0.6495191
[4,] -0.79044229 -0.7539981 1.6598820
[5,] -1.82992803 -1.7428481 0.9381942
[6,] -0.53057085 -0.7539981 0.7938566
[7,] 0.76878633 1.2237019 -1.2268693
[8,] 1.20190539 0.7292769 -0.6495191
attr(,"scaled:center")
AVG OBP K.AB
0.31125 0.33625 0.13500
attr(,"scaled:scale")
AVG OBP K.AB
0.11544170 0.10112757 0.06928203
> d[ dd[, 'AVG'] > 2 & dd[ ,'OBP'] >2 & dd[ ,'K.AB'] < 0.2 , ]
[1] Conf AVG OBP K.AB
<0 rows> (or 0-length row.names)
如果没有满足所有这些条件的行,那就不足为奇了,因为在小数据集中,缩放值2是不太可能的。
在Conf:
级别内应用比例> dd <- lapply(d[ ,c("AVG", "OBP", "K.AB")], function(x) ave(x, d[,"Conf"] , FUN=scale) )
> dd
$AVG
[1] 0.7071068 0.7071068 -0.7071068 -0.7071068 -0.7071068 -0.7071068 0.7071068 0.7071068
$OBP
[1] NaN 0.7071068 NaN -0.7071068 -0.7071068 -0.7071068 0.7071068 0.7071068
$K.AB
[1] 0.7071068 -0.7071068 -0.7071068 0.7071068 0.7071068 0.7071068 -0.7071068 -0.7071068
> data.frame(dd)
AVG OBP K.AB
1 0.7071068 NaN 0.7071068
2 0.7071068 0.7071068 -0.7071068
3 -0.7071068 NaN -0.7071068
4 -0.7071068 -0.7071068 0.7071068
5 -0.7071068 -0.7071068 0.7071068
6 -0.7071068 -0.7071068 0.7071068
7 0.7071068 0.7071068 -0.7071068
8 0.7071068 0.7071068 -0.7071068
我不认为它在这里工作得太好,因为提供的测试用例太小了。