基于多列计算数据框中的出现次数-R

Counting occurences in data frame based on multiple columns - R

我有一个 csv 文件 game.csv。这是数据集的样本

home   guest    result
team1  team2      w
team2  team3      l
team1  team3      l

R语言主场比赛时如何获取team1的胜场数?提前致谢。

您可以使用:

sum(df$home == 'team1' & df$result == 'w')

这类似于对数据帧进行子集化并计算行数。

nrow(subset(df, home == 'team1' & result == 'w'))

另一种选择是which/length

length(which(df$home == 'team1' & df$result == 'w'))