Tidyverse 根据名称更改值

Posted

技术标签:

【中文标题】Tidyverse 根据名称更改值【英文标题】:Tidyverse change values based on name 【发布时间】:2022-01-22 12:39:24 【问题描述】:

我有一个如下的数据框

library(tidyverse)
library(tidymodels)

#df <- read_csv("C:\\Users\\omarl\\OneDrive\\Escritorio\\games.csv")

df <- structure(list(gameId = 3326086514, creationTime = 1504279457970, 
                     gameDuration = 1949, seasonId = 9, winner = 1, firstBlood = 2, 
                     firstTower = 1, firstInhibitor = 1, firstBaron = 1, firstDragon = 1, 
                     firstRiftHerald = 2, t1_champ1id = 8, t1_champ1_sum1 = 12, 
                     t1_champ1_sum2 = 4, t1_champ2id = 432, t1_champ2_sum1 = 3, 
                     t1_champ2_sum2 = 4, t1_champ3id = 96, t1_champ3_sum1 = 4, 
                     t1_champ3_sum2 = 7, t1_champ4id = 11, t1_champ4_sum1 = 11, 
                     t1_champ4_sum2 = 6, t1_champ5id = 112, t1_champ5_sum1 = 4, 
                     t1_champ5_sum2 = 14, t1_towerKills = 11, t1_inhibitorKills = 1, 
                     t1_baronKills = 2, t1_dragonKills = 3, t1_riftHeraldKills = 0, 
                     t1_ban1 = 92, t1_ban2 = 40, t1_ban3 = 69, t1_ban4 = 119, 
                     t1_ban5 = 141, t2_champ1id = 104, t2_champ1_sum1 = 11, t2_champ1_sum2 = 4, 
                     t2_champ2id = 498, t2_champ2_sum1 = 4, t2_champ2_sum2 = 7, 
                     t2_champ3id = 122, t2_champ3_sum1 = 6, t2_champ3_sum2 = 4, 
                     t2_champ4id = 238, t2_champ4_sum1 = 14, t2_champ4_sum2 = 4, 
                     t2_champ5id = 412, t2_champ5_sum1 = 4, t2_champ5_sum2 = 3, 
                     t2_towerKills = 5, t2_inhibitorKills = 0, t2_baronKills = 0, 
                     t2_dragonKills = 1, t2_riftHeraldKills = 1, t2_ban1 = 114, 
                     t2_ban2 = 67, t2_ban3 = 43, t2_ban4 = 16, t2_ban5 = 51), row.names = c(NA, 
                                                                                            -1L), class = c("tbl_df", "tbl", "data.frame"))

df <- df %>%
  mutate(winner = ifelse(winner == 1, "team1", "team2")) %>%
  mutate(firstBlood = ifelse(firstBlood  == 1, "team1", "team2")) %>%
  mutate(firstTower = ifelse(firstTower == 1, "team1", "team2")) %>%
  mutate(firstInhibitor = ifelse(firstInhibitor == 1, "team1", "team2")) %>%
  mutate(firstBaron = ifelse(firstBaron == 1, "team1", "team2")) %>%
  mutate(firstDragon = ifelse(firstDragon == 1, "team1", "team2")) %>%
  mutate(firstRiftHerald = ifelse(firstRiftHerald == 1, "team1", "team2")) %>%
  select(-gameId, -creationTime) %>%
  filter(seasonId == 9) %>%
  select(gameDuration, winner, firstBlood, firstTower, firstInhibitor, firstBaron, firstDragon,
         firstRiftHerald)

如您所见,mutate 在这里确实是多余的,因为我正在复制每个变量的代码。有没有办法以编程方式将ifelse 应用于以firstt1 等开头的列?

【问题讨论】:

是的……检查 ?across 只是关于minimal reproducible example 的minimal 部分的一个小注释:tidymodels 没有在这个问题的范围内使用,所以你可以不考虑加载它.人们为了运行你的代码而少了一种依赖 【参考方案1】:

你可以试试

library(dplyr)

df %>%
  mutate(across(starts_with("t1")|starts_with("first"), ~ifelse(.x == 1, "team1", "team2")))

【讨论】:

使用map有什么不同吗?【参考方案2】:

Park 给出了一个最好的(一个班轮)解决方案。但是,如果您想查看其他选项,我们可以通过使用dplyr 中的其他功能来做到这一点:

df %>% 
  gather(key, value, firstBlood:t1_ban5) %>% 
  mutate(value = ifelse(value == 1, "team1", "team2")) %>% 
  spread(key, value) %>% 
  select(-gameId, -creationTime) %>%
  filter(seasonId == 9) %>%
  select(gameDuration, winner, firstBlood, firstTower, firstInhibitor, firstBaron, firstDragon,
         firstRiftHerald)

【讨论】:

以上是关于Tidyverse 根据名称更改值的主要内容,如果未能解决你的问题,请参考以下文章

根据列表名称变异向量

使用Excel中的“名称管理器”动态更改图像

Word宏:根据图像比例更改页面方向

MVC-如何根据在同一视图文本框中输入的值设置复选框的显示名称

从下拉列表中选择值,第二个下拉列表自动更改

根据条件编译符号更改exe的名称