我有以下数据集:
Name Make_Miss Half
Player A 1 1
Player B 1 1
Player A 0 2
Player A 0 1
Player A 1 1
Player B 0 2
名称是玩家的名字,Make_Miss是玩家是否进行了射击,而Half是射击的一半。我目前正在使用以下代码来计算前半部分的计数。
代码:
dt[ , Player_First_Made := .N, by = list(dt$Name == "Player A" & dt$Half == 1 & dt$Make_Miss == 1)]
输出:
Name Make_Miss Half Player_First_Made
Player A 1 1 2
Player B 1 1 4
Player A 0 2 4
Player A 0 1 4
Player A 1 1 2
Player B 0 2 4
这里发生的事情是,无论玩家A在Make_Miss列中输入为0,Player_First_Made列中的相应行都会被分配与列表中的条件不匹配的镜头数(即名称) !=玩家A或半!= 1或Make_Miss!= 1);但是,我的愿望如下:
Name Make_Miss Half Player_First_Made
Player A 1 1 2
Player B 1 1 4
Player A 0 2 2
Player A 0 1 2
Player A 1 1 2
Player B 0 2 4
我希望与Name = Player A匹配的行总是具有他们在上半场制作的镜头数量。是否有某种data.table语法我可以指定这个赋值?
答案 0 :(得分:1)
正如@ chinsoon12指出的那样,你提供的数据并不合理。但是,这是一个使用dplyr的方法,我认为会给你你想要的东西......
library(dplyr)
# Make some data
DATA <- data.frame(Name = c("Player A", "Player B", "Player C",
"Player A", "Player A", "Player B"), Make_Miss = c(1,1,0,0,1,0),
Half = c(1,1,2,1,2,2))
# Use dplyr to calculate the sums of 'Half' for each player
OUT <- DATA %>% group_by(Name) %>% mutate(Player_First_Made = sum(Half))
# Check the output
> OUT
# A tibble: 6 x 4
# Groups: Name [3]
Name Make_Miss Half Player_First_Made
<fct> <dbl> <dbl> <dbl>
1 Player A 1 1 4
2 Player B 1 1 3
3 Player C 0 2 2
4 Player A 0 1 4
5 Player A 1 2 4
6 Player B 0 2 3
如果这不是您想要的,那么请编辑您的问题以使其更清晰。
答案 1 :(得分:1)
执行此操作的data.table方法是:
dat[Half == 1, .(Player_First_Made = sum(Make_Miss)), .(Name)
][dat, on = c('Name')]
第一行计算每个玩家(sum(Make_Miss)
)在上半场(.(Name)
)拍摄的次数(Half == 1
)。
第二行将结果聚合表从上面的步骤连接回原始数据集。
这是我使用的示例数据:
dat <-
data.table(
Name = c('A', 'B'),
Make_Miss = round(runif(30, 0, 1)),
Half = round(runif(30, 1, 2))
)