簡體   English   中英

有條件地創建一個新變量

[英]Conditionally creating a new variable

我有以下 data.frame。 我需要創建第六個變量 (SAT_NEWS),如下所示:如果在四個變量中的三個 ($medwell_.) 中,受訪者回答“很好”或“有點好”,則新變量的值為 SAT,否則為是 NON_SAT。

'data.frame':   41953 obs. of  5 variables:
 $ trust_gov       : Factor w/ 6 levels "A lot","Somewhat",..: 1 2 2 2 1 2 4 2 2 2 ...
 $ medwell_accuracy: Factor w/ 7 levels "Very well","Somewhat well",..: 2 4 2 3 4 2 1 1 1 1 ...
 $ medwell_leaders : Factor w/ 7 levels "Very well","Somewhat well",..: 2 3 2 4 4 3 1 2 1 1 ...
 $ medwell_unbiased: Factor w/ 7 levels "Very well","Somewhat well",..: 4 4 2 4 3 2 1 2 1 3 ...
 $ medwell_coverage: Factor w/ 7 levels "Very well","Somewhat well",..: 2 4 1 3 3 2 1 1 2 3 ...
 - attr(*, "variable.labels")= Named chr  "ID. Respondent ID" "Survey" "Country" "QSPLIT. Split form A or B" ...
  ..- attr(*, "names")= chr  "ID" "survey" "Country" "qsplit" ...
 - attr(*, "codepage")= int 65001

你能幫助我嗎?

不幸的是,數據幀沒有%in%方法,因此需要做一些額外的工作。 使用基礎 R 我們可以使用

nm <- grep("medwell_", names(df))
num <- colSums(apply(df[, nm], 1, `%in%`, c("Very well", "Somewhat well")))
df$new <- ifelse(num == 3, "SAT", "NON_SAT")

而使用dplyr我們有

df %>% 
  mutate(
    new = ifelse(
      select(., contains("medwell_")) %>% 
        map2_dfr(list(c("Very well", "Somewhat well")), `%in%`) %>%
        rowSums() == 3, "SAT", "NON_SAT"
    )
  )

暫無
暫無

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

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