【问题标题】:Logical operation to fill new colum in dataframe with character values用字符值填充数据框中的新列的逻辑操作
【发布时间】:2017-12-29 12:54:20
【问题描述】:

我有一个不同国家宽带数据的数据框(以 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”,其中事件均为“yes”; “否”、“FT Doubleplay”,其中事件为“否”; “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

为此,我需要一个逻辑操作来分配新值“Singelplay、Doubleplay、...”而不删除/覆盖现有数据。我已经搜索过类似的东西,但无法找到/理解这些操作实际上是如何工作的..

我是这个社区的新手,也是 R 的新手(这里的第一篇文章)。希望有人可以提供帮助。

【问题讨论】:

    标签: r dataframe logical-operators


    【解决方案1】:

    我们可以使用 中的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
    

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

    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)
    

    【讨论】:

    • 非常感谢!完美运行
    猜你喜欢
    • 1970-01-01
    • 2019-09-30
    • 1970-01-01
    • 1970-01-01
    • 2019-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多