我希望有人能帮助我解决我的问题。我有一个数据框,我希望基于现有数据框添加2组新值。
我希望在季节列下确定新数据。
这里是:
:
print (f(T,v,2))
array([[[1., 1., 1.],
[2., 2., 2.],
[3., 3., 3.]],
[[1., 1., 1.],
[2., 2., 2.],
[3., 3., 3.]],
[[1., 1., 1.],
[2., 2., 2.],
[3., 3., 3.]]])
答案 0 :(得分:0)
您可以这样做:
full_table_raw %>%
# add a new column with the weights to apply
mutate(weight = ifelse(season == 2016, .2, .4)) %>%
# group_by, and then compute your averages
group_by(stat) %>%
summarize(
average = sum(value) / 3,
weighted_average = sum(value * weight))
哪个给
# A tibble: 4 x 3
stat weighted_average average
<chr> <dbl> <dbl>
1 HR 83.8 85.7
2 R 258 264
3 RBI 275 281
4 SB 35.0 32.3
如果您不想将摘要weighted_average
和average
添加为列,而无需计算摘要,则可以简单地将summarize
替换为mutate
# A tibble: 36 x 9
# Groups: stat [4]
playerID season stat points ranks value weight average weighted_average
<chr> <int> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 abreujo02 2014 HR 3.00 1.00 36.0 0.400 85.7 83.8
2 abreujo02 2014 R 2.00 2.00 80.0 0.400 264 258
3 abreujo02 2014 RBI 3.00 1.00 107 0.400 281 275
4 abreujo02 2014 SB 2.00 2.00 3.00 0.400 32.3 35.0
5 abreujo02 2015 HR 2.00 2.00 30.0 0.400 85.7 83.8
6 abreujo02 2015 R 1.00 3.00 88.0 0.400 264 258
7 abreujo02 2015 RBI 2.00 2.00 101 0.400 281 275
...
请注意,我们可以使用最后的%>% select(-weight)
删除添加的列weight