簡體   English   中英

根據 R 中的多個條件對新列進行變異

[英]Mutate a new column based on multiple conditions in R

假設以下數據集:

    df <- structure(list(id = 1:9, city = structure(c(1L, 7L, 2L, 6L, 4L, 
9L, 3L, 8L, 5L), .Label = c("bj", "gz", "lz", "nj", "sh", "sz", 
"tj", "wh", "xa"), class = "factor")), class = "data.frame", row.names = c(NA, 
-9L))

在此處輸入圖像描述

如何根據條件創建新的列direction

如果city在列表['bj', 'tj']中,則返回north作為direction ,如果在['sz', 'nj', 'sh']中,則返回east ,如果在['xa', 'lz']返回west ,如果在['wh']中返回center ,如果在['gz', 'sz']中返回south

預期的結果將是這樣的:

在此處輸入圖像描述

我的代碼:

df %>%
  filter(city %in% c('bj', 'tj')) %>%
  mutate(direction = 'north')

出去:

在此處輸入圖像描述

使用case_when

library(dplyr)

df %>%
  mutate(direction = case_when(city %in% c('bj', 'tj') ~ 'north', 
                               city %in% c('sz', 'nj', 'sh') ~ 'east', 
                               city %in% c('xa', 'lz') ~ 'west', 
                               city %in% c('wh') ~ 'center', 
                               city %in% c('gz', 'sz') ~ 'south', 
                               ))

#  id city direction
#1  1   bj     north
#2  2   tj     north
#3  3   gz     south
#4  4   sz      east
#5  5   nj      east
#6  6   xa      west
#7  7   lz      west
#8  8   wh    center
#9  9   sh      east

您可以使用基本的 R data.frame 操作以簡單的方式完成此操作:

df$direction <- ""
df[df$city %in% c('bj', 'tj'), "direction"] <- "north"
df[df$city %in% c('sz', 'nj', 'sh'),"direction"] <- "east"
df[df$city %in% c('xa', 'lz'), "direction"] <- "west"
df[df$city %in% c('wh'), "direction"] <- "center"
df[df$city %in% c('gz', 'sz'), "direction"] <- "south"
 
df
  id city direction
1  1   bj     north
2  2   tj     north
3  3   gz     south
4  4   sz     south
5  5   nj      east
6  6   xa      west
7  7   lz      west
8  8   wh    center
9  9   sh      east

使用嵌套的 ifelse 語句也可以完成這項工作。

df$direction=ifelse(df$city %in% c("bj","tj"), yes = "north",
          ifelse(df$city %in% c('sz', 'nj', 'sh'), yes = "east",
          ifelse(df$city %in% c("xa", "lz"), yes = "west",
          ifelse(df$city %in% c("gz", "sz"), yes = "south", no = "center"))))

您可以先嘗試stack創建字典,然后匹配城市,例如,

d <- stack(
  list(
    north = c("bj", "tj"),
    east = c("sz", "nj", "sh"),
    west = c("xa", "lz"),
    center = "wh",
    south = c("gz", "sz")
  )
)

df <- transform(
  df,
  direction = d$ind[match(city,d$values)]
)

這使

  id city direction
1  1   bj     north
2  2   tj     north
3  3   gz     south
4  4   sz      east
5  5   nj      east
6  6   xa      west
7  7   lz      west
8  8   wh    center
9  9   sh      east

我還嘗試了另一種 mutate 解決方案,但錯誤消息與錯誤符號相同

這里我先加載了 dplyr 庫。

library(dplyr)
new_soccer_referee %>%
  mutate(postion_new = case_when (position %in% c("Right Fullback", "Left Fullback", "Center Back", "Defensive Midfielder") ~ "Defense",
                                  position %in% c("Right Midfielder", "Left Midfielder", "Center Midfielder") ~ "Midfield",
                                  position %in% c("Attacking Midfielder", "Right Winger", "Left Winger", "Center Forward") ~ "Offense",
                                  ))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM