Set certain values to NA with dplyr

前端 未结 5 1185
囚心锁ツ
囚心锁ツ 2020-11-28 21:20

I\'m trying to figure out a simple way to do something like this with dplyr (data set = dat, variable = x):

day$x[dat$x<0]=NA

Should be

5条回答
  •  迷失自我
    2020-11-28 21:39

    You can use the is.na<- function:

    dat %>% mutate(x = "is.na<-"(x, x < 0))
    

    Or you can use mathematical operators:

    dat %>% mutate(x = NA ^ (x < 0) * x)
    

提交回复
热议问题