gpt4 book ai didi

r - 按字母顺序在两列中创建组合信息的新列

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

我有一个足球队数据集,如下所示:

Home_team Away_team Home_score Away_score
Arsenal Chelsea 1 3
Manchester U Blackburn 2 9
Liverpool Leeds 0 8
Chelsea Arsenal 4 1

我想对所涉及的球队进行分组,无论哪支球队在主场和客场比赛。例如,如果切尔西对阵阿森纳,无论比赛是在切尔西还是在阿森纳,我都希望新列“teams_involved”是阿森纳 - 切尔西。我的猜测是这样做的方法是按字母顺序将这些团队添加到新列中,但我不知道该怎么做。

期望的输出:
Home_team Away_team Home_score Away_score teams_involved
Arsenal Chelsea 1 3 Arsenal - Chelsea
Manchester U Blackburn 2 9 Blackburn - Manchester U
Liverpool Leeds 0 8 Leeds - Liverpool
Chelsea Arsenal 4 1 Arsenal - Chelsea

我之所以要这样做,是因为我可以看到每支球队对阵特定球队的胜利次数,无论比赛地点如何。

最佳答案

df = read.table(text = "
Home_team Away_team Home_score Away_score
Arsenal Chelsea 1 3
ManchesterU Blackburn 2 9
Liverpool Leeds 0 8
Chelsea Arsenal 4 1
", header=T, stringsAsFactors=F)

library(dplyr)

df %>%
rowwise() %>% # for each row
mutate(Teams = paste(sort(c(Home_team, Away_team)), collapse = " - ")) %>% # sort the teams alphabetically and then combine them separating with -
ungroup() # forget the row grouping

# # A tibble: 4 x 5
# Home_team Away_team Home_score Away_score Teams
# <chr> <chr> <int> <int> <chr>
# 1 Arsenal Chelsea 1 3 Arsenal - Chelsea
# 2 ManchesterU Blackburn 2 9 Blackburn - ManchesterU
# 3 Liverpool Leeds 0 8 Leeds - Liverpool
# 4 Chelsea Arsenal 4 1 Arsenal - Chelsea

没有 rowwise 的替代解决方案:
# create function and vectorize it
f = function(x,y) {paste(sort(c(x, y)), collapse = " - ")}
f = Vectorize(f)

# apply function to your dataset
df %>% mutate(Teams = f(Home_team, Away_team))

关于r - 按字母顺序在两列中创建组合信息的新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51769084/

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