Using if else on a dataframe across multiple columns

后端 未结 6 1482
情书的邮戳
情书的邮戳 2021-01-14 21:43

I have a large dataset of samples with descriptors of whether the sample is viable - it looks (kind of) like this, where \'desc\' is the description column and \'blank\' ind

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-14 22:17

    Here's another dplyr solution with a small custom function and mutate_each().

    library(dplyr)
    
    f <- function(x) if_else(dat$desc == "blank", NA_real_, x)
    dat %>% 
      mutate_each(funs(f), -desc)
    #>      desc        x        y        z
    #> 1   blank       NA       NA       NA
    #> 2   blank       NA       NA       NA
    #> 3  sample 3.624941 6.430955 5.486632
    #> 4  sample 3.236359 4.935453 4.319202
    #> 5   blank       NA       NA       NA
    #> 6   blank       NA       NA       NA
    #> 7   blank       NA       NA       NA
    #> 8  sample 5.058725 6.751650 4.750529
    #> 9  sample 5.837206 4.323562 4.914780
    #> 10  blank       NA       NA       NA
    

提交回复
热议问题