首先,我对播放器的数据取平均值并按比例缩小。
player <- player %>%
group_by(NM) %>%
summarise_all(funs(mean(., na.rm = TRUE)))
这就是结果。
head(player)
# A tibble: 6 x 26
NM NO MIN `2PTM` `2PTA` `2PT(%)` `3PTM` `3PTA` `3PT(%)` FGM FGA `FG(%)` FTM FTA `FT(%)` OR DR REB
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 a.~ 6 918. 0.32 0.72 14.8 0.44 1.9 13.7 0.76 2.62 17.9 0.18 0.32 6.5 0.38 0.64 1.02
2 b.~ 13 1681. 3.41 6.43 54.5 0.574 2.13 19.7 3.98 8.56 47.4 0.907 1.33 35.5 2.04 3.22 5.26
3 c.~ 31 704. 0.921 2.16 36.0 0.421 1.18 21.1 1.34 3.34 38.6 0.447 0.658 19.7 0.526 1.16 1.68
4 d.~ 21 968. 0.885 1.88 32.6 0.846 2.08 22.1 1.73 3.96 35.3 1.15 1.35 39.1 0.462 1.04 1.5
5 e.~ 9 105. 0.118 0.294 8.82 0.176 0.412 11.8 0.294 0.706 18.6 0.0588 0.118 2.94 0.294 0.176 0.471
6 f.~ 2 509. 0.571 1.57 21.4 0.286 1.29 10 0.857 2.86 17 0.286 0.714 21.4 0.143 0.714 0.857
# ... with 8 more variables: AST <dbl>, TO <dbl>, STL <dbl>, BS <dbl>, PF <dbl>, FO <dbl>, PP <dbl>, PTS <dbl>
然后,我使用了mutate函数。但是它没有添加列。我听说小声很有限。因此,我将其转换为数据框。
> head(as.data.frame(player))
NM NO MIN 2PTM 2PTA 2PT(%) 3PTM 3PTA 3PT(%) FGM FGA FG(%) FTM
1 a 6 918.0600 0.3200000 0.7200000 14.800000 0.4400000 1.9000000 13.74000 0.7600000 2.6200000 17.94000 0.18000000
2 b 13 1680.8889 3.4074074 6.4259259 54.462963 0.5740741 2.1296296 19.72222 3.9814815 8.5555556 47.42593 0.90740741
3 c 31 704.0526 0.9210526 2.1578947 35.973684 0.4210526 1.1842105 21.05263 1.3421053 3.3421053 38.63158 0.44736842
4 d 21 967.5000 0.8846154 1.8846154 32.576923 0.8461538 2.0769231 22.11538 1.7307692 3.9615385 35.26923 1.15384615
5 e 9 105.1765 0.1176471 0.2941176 8.823529 0.1764706 0.4117647 11.76471 0.2941176 0.7058824 18.64706 0.05882353
6 f 2 508.5714 0.5714286 1.5714286 21.428571 0.2857143 1.2857143 10.00000 0.8571429 2.8571429 17.00000 0.28571429
FTA FT(%) OR DR REB AST TO STL BS PF FO PP
1 0.3200000 6.500000 0.3800000 0.6400000 1.0200000 0.8000000 0.2200000 0.44000000 0.00000000 1.9400000 0.64000000 0.6400000
2 1.3333333 35.481481 2.0370370 3.2222222 5.2592593 1.2592593 0.8333333 0.74074074 0.42592593 3.3888889 1.70370370 4.5925926
3 0.6578947 19.736842 0.5263158 1.1578947 1.6842105 0.4736842 0.1842105 0.28947368 0.02631579 1.3684211 0.65789474 1.6842105
4 1.3461538 39.115385 0.4615385 1.0384615 1.5000000 0.9230769 0.8846154 0.50000000 0.03846154 2.1923077 2.46153846 1.6153846
5 0.1176471 2.941176 0.2941176 0.1764706 0.4705882 0.0000000 0.1176471 0.05882353 0.00000000 0.1176471 0.05882353 0.1176471
6 0.7142857 21.428571 0.1428571 0.7142857 0.8571429 0.7142857 0.5714286 0.28571429 0.00000000 1.4285714 0.57142857 0.8571429
PTS
1 2.1400000
2 9.4444444
3 3.5526316
4 5.4615385
5 0.8235294
6 2.2857143
问:
1.为什么我不能使用变异功能?实际上,因为没有错误代码,所以它可以工作。但未添加任何列。
答案 0 :(得分:-1)
如果您希望从该转换中获取一个数据框,则可以尝试:
already_a_DF <- player %>%
group_by(NM) %>%
summarise_all(funs(mean(., na.rm = TRUE))) %>% as.data.frame