我在 R 中有兩個不同的變數。第一個(“candimmi”)代表政治候選人對移民的看法。第二個變數(voterimmi)代表選民對移民的看法。這兩個變數具有相同的 3 個級別,即反移民、中間或親移民。
我的問題是我想創建一個新變數來說明選民和政治候選人之間是否一致。新變數中的水平將被稱為“都反移民”、“都中間”、“都支持移民”和“不匹配”。
你們中的任何人都可以給我一些關于如何做到這一點的建議嗎?
提前致謝!
最好的,馬耳他
我已經嘗試過尋找解決方案,但在網上找不到我的問題的任何答案。
uj5u.com熱心網友回復:
您可以使用case_when,這只是dplyr的版本ifelse:
set.seed(05062020)
library(dplyr)
responses <- c("Anti","Intermed","Pro")
df <- data.frame(candidate = sample(responses, 10, replace = TRUE),
voter = sample(responses, 10, replace = TRUE))
df2 <- df %>% mutate(result = case_when(candidate %in% "Anti" & voter %in% "Anti" ~ "Both Anti",
candidate %in% "Intermed" & voter %in% "Intermed" ~ "Both Intermed",
candidate %in% "Pro" & voter %in% "Pro" ~ "Both Pro",
candidate != voter ~ "Discordant"))
# candidate voter result
# 1 Pro Intermed Discordant
# 2 Anti Anti Both Anti
# 3 Pro Pro Both Pro
# 4 Pro Anti Discordant
# 5 Pro Anti Discordant
# 6 Pro Pro Both Pro
# 7 Pro Intermed Discordant
# 8 Intermed Pro Discordant
# 9 Intermed Intermed Both Intermed
# 10 Anti Pro Discordant
一種基本的 R 方法是使用嵌套ifelse陳述句:
df$result <- ifelse(df$candidate %in% "Anti" & df$voter %in% "Anti", "Both Anti",
ifelse(df$candidate %in% "Intermed" & df$voter %in% "Intermed", "Both Intermed",
ifelse(df$candidate %in% "Pro" & df$voter %in% "Pro", "Both Pro",
ifelse(df$candidate != df$voter, "Discordant", NA))))
# > df
# candidate voter result
# 1 Pro Intermed Discordant
# 2 Anti Anti Both Anti
# 3 Pro Pro Both Pro
# 4 Pro Anti Discordant
# 5 Pro Anti Discordant
# 6 Pro Pro Both Pro
# 7 Pro Intermed Discordant
# 8 Intermed Pro Discordant
# 9 Intermed Intermed Both Intermed
# 10 Anti Pro Discordant
uj5u.com熱心網友回復:
這是使用基本 R 函式factor和interaction(使用具有不同隨機種子的 @jpsmith 示例 data.frame )的簡單方法。其核心是,interaction將自動創建一個具有組合級別的新因子,然后您可以根據需要重命名它們(可能對許多因子級別有用)。
set.seed(234) # fixed random seed for reproducibility
responses <- c("Anti", "Intermed", "Pro")
congruence <- c("both anti-immigrant", "both intermediate", "both pro-immigration", "mismatch")
df <- data.frame(candidate = sample(responses, 10, replace = TRUE),
voter = sample(responses, 10, replace = TRUE))
df$candidate <- factor(df$candidate, levels=responses) # make sure you have all the levels
df$voter <- factor(df$voter, levels=responses) # make sure you have all the levels
df$congruence <- with(df, interaction(candidate, voter)) # create new factor representing both levels
levels(df$congruence) <- congruence[c(1,4,4,4,2,4,4,4,3)] # match up factor levels to rename
df
#> candidate voter congruence
#> 1 Anti Pro mismatch
#> 2 Pro Pro both pro-immigration
#> 3 Intermed Intermed both intermediate
#> 4 Intermed Pro mismatch
#> 5 Intermed Intermed both intermediate
#> 6 Intermed Intermed both intermediate
#> 7 Anti Anti both anti-immigrant
#> 8 Anti Anti both anti-immigrant
#> 9 Pro Intermed mismatch
#> 10 Intermed Pro mismatch
由reprex 包創建于 2022-04-05 (v2.0.1)
uj5u.com熱心網友回復:
其他兩個答案都可以正常作業,但最簡單的解決方案是只使用一個ifelse(). 下面我首先創建一些示例資料,然后根據您的喜好展示如何ifelse()在 tidyverse 或 base R 中使用。
library(tidyverse)
# Create data sample
d <- crossing(
candimmi = c("anti", "inter", "pro"),
voterimmi = candimmi
)
d |>
mutate(new_tidy = ifelse(candimmi != voterimmi,
"mismatch",
str_c("both ", candimmi)))
#> # A tibble: 9 × 3
#> candimmi voterimmi new_tidy
#> <chr> <chr> <chr>
#> 1 anti anti both anti
#> 2 anti inter mismatch
#> 3 anti pro mismatch
#> 4 inter anti mismatch
#> 5 inter inter both inter
#> 6 inter pro mismatch
#> 7 pro anti mismatch
#> 8 pro inter mismatch
#> 9 pro pro both pro
d$new_base <- ifelse(d$candimmi != d$voterimmi,
"mismatch",
paste("both", d$candimmi))
d
#> # A tibble: 9 × 3
#> candimmi voterimmi new_base
#> <chr> <chr> <chr>
#> 1 anti anti both anti
#> 2 anti inter mismatch
#> 3 anti pro mismatch
#> 4 inter anti mismatch
#> 5 inter inter both inter
#> 6 inter pro mismatch
#> 7 pro anti mismatch
#> 8 pro inter mismatch
#> 9 pro pro both pro
由reprex 包創建于 2022-04-05 (v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/456478.html
上一篇:操作順序如何嚴重改變代碼速度?
