用字符值填充数据框中新列的逻辑操作

Logical operation to fill new colum in dataframe with character values

我有一个关于各国宽带数据的数据框(以AT_df为例)。 "ofWithTV" 和 "ofWithFT" 列属于字符类型,表示每个案例(宽带报价)是否附带捆绑电视接入或固定电话接入,有(或没有)两者。

ofWithTV    ofWithFT  
no          no
no          no
no          no
yes         no
yes         no
no          no
no          yes
no          yes
no          yes
no          yes
yes         yes
yes         yes

我想创建一个新列 "ofProduct",其中这些案例应称为 "Singleplay",其中两个事件均为 "no","TV Doubleplay",其中事件为 [=26] =]; "no", "FT Doubleplay" 其中事件是 "no"; "yes" 和 "Tripleplay",其中两个事件都是 "yes"。像这样:

ofWithTV    ofWithFT   ofProduct
no          no         Singleplay
no          no         Singleplay
no          no         Singleplay
yes         no         TV Doubleplay 
yes         no         TV Doubleplay
no          no         Singleplay
no          yes        FT Doubleplay
no          yes        FT Doubleplay
no          yes        FT Doubleplay
no          yes        FT Doubleplay
yes         yes        Tripleplay
yes         yes        Tripleplay

为此,我需要一个逻辑操作来在没有 deleting/overwriting 现有数据的情况下分配新值 "Singelplay, Doubleplay,..."。我已经搜索过类似的东西,但无法 find/understand 这些操作的实际工作方式..

我是这个社区的新手,也是 R 的新手(这里是第一个 post)。希望有人能帮忙。

我们可以使用 中的 case_when

library(dplyr)

dat2 <- dat %>%
  mutate(ofProduct = case_when(
    ofWithTV %in% "no" & ofWithFT %in% "no"   ~ "Singleplay",
    ofWithTV %in% "yes" & ofWithFT %in% "no"  ~ "TV Doubleplay",
    ofWithTV %in% "no" & ofWithFT %in% "yes"  ~ "FT Doubleplay",
    ofWithTV %in% "yes" & ofWithFT %in% "yes" ~ "Tripleplay"
  ))
dat2
#    ofWithTV ofWithFT     ofProduct
# 1        no       no    Singleplay
# 2        no       no    Singleplay
# 3        no       no    Singleplay
# 4       yes       no TV Doubleplay
# 5       yes       no TV Doubleplay
# 6        no       no    Singleplay
# 7        no      yes FT Doubleplay
# 8        no      yes FT Doubleplay
# 9        no      yes FT Doubleplay
# 10       no      yes FT Doubleplay
# 11      yes      yes    Tripleplay
# 12      yes      yes    Tripleplay

或者我们可以先创建一个查找 table,然后将 table 连接到原始数据框。

library(dplyr)
library(tibble)

look_up <- tribble(
  ~ofWithTV, ~ofWithFT, ~ofProduct,
  "no"     , "no"     , "Singleplay",
  "yes"    , "no"     , "TV Doubleplay",
  "no"     , "yes"    , "FT Doubleplay",
  "yes"    , "yes"    , "Tripleplay"
)

dat3 <- dat %>%
  left_join(look_up, by = c('ofWithTV', "ofWithFT"))
dat3
#    ofWithTV ofWithFT     ofProduct
# 1        no       no    Singleplay
# 2        no       no    Singleplay
# 3        no       no    Singleplay
# 4       yes       no TV Doubleplay
# 5       yes       no TV Doubleplay
# 6        no       no    Singleplay
# 7        no      yes FT Doubleplay
# 8        no      yes FT Doubleplay
# 9        no      yes FT Doubleplay
# 10       no      yes FT Doubleplay
# 11      yes      yes    Tripleplay
# 12      yes      yes    Tripleplay

数据

dat <- read.table(text = "ofWithTV    ofWithFT  
no          no
no          no
no          no
yes         no
yes         no
no          no
no          yes
no          yes
no          yes
no          yes
yes         yes
yes         yes",
                  header = TRUE, stringsAsFactors = FALSE)