R: Replace multiple values in multiple columns of dataframes with NA

前端 未结 6 647
情书的邮戳
情书的邮戳 2020-12-11 01:31

I am trying to achieve something similar to this question but with multiple values that must be replaced by NA, and in large dataset.

df <- data.frame(nam         


        
6条回答
  •  情深已故
    2020-12-11 01:54

    I think dplyr is very well-suited for this task.
    Using replace() as suggested by @thelatemail, you could do something like this:

    library("dplyr")
    df <- df %>% 
      mutate_at(vars(starts_with("var")),
                funs(replace(., . %in% c(3, 4), NA)))
    
    df
    #   name foo var1 var2
    # 1    a   1    1   NA
    # 2    a   2    2   NA
    # 3    a   3   NA   NA
    # 4    b   4   NA   NA
    # 5    b   5    5   NA
    # 6    b   6    6   NA
    # 7    c   7    7    5
    # 8    c   8    8    5
    # 9    c   9    9    5
    

提交回复
热议问题