gpt4 book ai didi

r - 我如何在 R 中创建一个 data.frame 玩家在游戏中一起开始的次数

转载 作者:行者123 更新时间:2023-12-04 12:35:30 25 4
gpt4 key购买 nike

我刚刚开始研究网络分析,并想从创建一个数据框架开始,该框架显示团队中的篮球运动员一起开始的频率

理想情况下,我想合并来自 purrr 的 map 功能

所以以此作为输入

game_1 <- c("Andy","Bob","Chris","Doug","Evan") 
game_2 <- c("Andy","Chris","Evan","Fred","George")

我想要这样的结果
n_1  n_2 games

Andy Bob 1
Andy Chris 2
Andy Doug 1
Andy Evan 2
Andy Fred 1
Andy George 1
Bob Chris 1
Bob Doug 1
Bob Evan 1
Chris Doug 1
Chris Evan 2
Chris Fred 1
Chris George 1
Doug Evan 1
Evan Fred 1
Evan George 1
Fred George 1

最佳答案

library(dplyr)

# get combinations from game_1
g1 <- combn(game_1, 2) %>% t

# get combinations from game_2
g2 <- combn(game_2, 2) %>% t

# bind both in a dataframe and count pairs
g1 %>%
rbind.data.frame(g2) %>%
group_by(V1, V2) %>%
summarise(games = n())

# A tibble: 17 x 3
# Groups: V1 [?]
V1 V2 games
<fctr> <fctr> <int>
1 Andy Bob 1
2 Andy Chris 2
3 Andy Doug 1
4 Andy Evan 2
5 Andy Fred 1
6 Andy George 1
7 Bob Chris 1
8 Bob Doug 1
9 Bob Evan 1
10 Chris Doug 1
11 Chris Evan 2
12 Chris Fred 1
13 Chris George 1
14 Doug Evan 1
15 Evan Fred 1
16 Evan George 1
17 Fred George 1

关于r - 我如何在 R 中创建一个 data.frame 玩家在游戏中一起开始的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49779908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com