拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 Tidyverse根据名称更改值

Tidyverse根据名称更改值

白鹭 - 2022-03-01 2121 0 0

我有一个资料框如下

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等编程?

uj5u.com热心网友回复:

你可以试试

library(dplyr)

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

uj5u.com热心网友回复:

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)
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *