Using if else on a dataframe across multiple columns

后端 未结 6 1344
情书的邮戳
情书的邮戳 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:36

    You can use dplyr and a custom function to mutate values on certain conditions.

    `

    library(dplyr)
    mutate_cond <- function(.data, condition, ..., envir = parent.frame()) {
            condition <- eval(substitute(condition), .data, envir)
            .data[condition, ] <- .data[condition, ] %>% mutate(...)
            .data
    }
    data <- data %>% 
    mutate_cond( desc == "blank", x = NA, y = NA, z = NA)
    

    `

提交回复
热议问题