(R)如何从两个不同的数据集中查看事件发生的次数

时间:2017-03-25 10:27:22

标签: r

好的,这是我的数据库

Game    Player    Hit   Something else
1       Trout     TRUE  1
1       Pujols    FALSE 5
1       Hunter    TRUE  6
1       Trout     TRUE  7
2       Hunter    TRUE  23
2       Pujols    FALSE 0
等等,它下降到162场比赛。所以如果我有不同的数据框

Player    Number of Games with at least one hit
Trout
Hunter
Pujols

我如何获得第二列。我需要一个过滤玩家和游戏的声明,因为为每个玩家和游戏制作一个子集需要太长时间。我需要计算一个玩家获得命中的次数,但仅限于每个游戏。 谢谢

1 个答案:

答案 0 :(得分:0)

如果我正确理解了这个问题,以下内容适用于% Define the length of the line r=3; % Define the starting point coordinates x_start=1; y_start=3; % Define the set of angles (in radiants) theta=[0:30]; % Open a Figure figure % Add the axes axes daspect([1 1 1]) hold on % Plot the reference line x_ref=[x_start r+x_start] y_ref=[y_start y_start] xlim([0 r+x_start]) ylim([0 r+y_start]) plot(x_ref,y_ref,'--b','linewidth',2) grid minor % Plot the line for i=1:length(theta) % Plot the line pl_h=plot([x_start x_start+r*cosd(theta(i))], ... [y_start y_start+r*sind(theta(i))],'r','linewidth',2) xlim([0 r+x_start]) ylim([0 r+y_start]) % Add a string displaying the value of the current angle tx_h=text(1,2,['Angle= ' num2str(theta(i)) ' [Deg]']) pause(1) delete([tx_h pl_h]) end 。只需看看Hit的真实值,删除任何重复项(即多次击中的游戏),然后计算每个玩家的行数。

dplyr

这将为您提供您提到的第二个数据框。或者,如果它已存在(library(dplyr) df <- data.frame(Game=c(1,1,1,1,2,2),Player=c("Trout","Pujols","Hunter","Trout","Hunter","Pujols"),Hit=c(TRUE,FALSE,TRUE,TRUE,TRUE,FALSE)) df2 <- df %>% filter(Hit) %>% distinct(Game,Player) %>% count(Player) ),您可以将新数据与df3合并到其中。