If/else to replace certain values with NA

Hello!

I'm trying to replace values with NA that do not meet a certain condition. For the below Kd.df dataframe, if the Kd.df$r2vector is less than 0.7, I want to replace the Kd.df$Kdvector value with NA. I tried using an if/else statement:

Kd.df <- if(Kd.df$r2vector){
  Kd.df$Kdvector = NA
}

But it didn't work. I got the error message:

Warning message:
In if (Kd.df$r2vector) { :
  the condition has length > 1 and only the first element will be used

Can someone advise? Below is my dataframe.

Kd.df <- structure(list(sIDs = c("GBA20-10501", "GBA20-10506", "GBA20-10507", 
"GBA20-10508", "GBA20-10509", "GBA20-10510", "GBA20-10511", "GBA20-10512", 
"GBA20-10513", "GBA20-10516", "GBA20-10517", "GBA20-10518", "GBA20-10519", 
"GBA20-10520", "GBA20-10521", "GBA20-10522", "GBA20-10523", "GBA20-10524", 
"GBA20-10525", "GBA20-10526", "GBA20-10527", "GBA20-10528", "GBA20-10529", 
"GBA20-10530", "GBA20-10531", "GBA20-10532", "GBA20-10533", "GBA20-10534", 
"GBA20-10535", "GBA20-10536", "GBA20-10537", "GBA20-10538", "GBA20-10539", 
"GBA20-10540", "GBA20-10541", "GBA20-10542", "ISA20-13", "ISA20-16", 
"ISA20-23", "ISA20-27", "ISA20-30", "ISA20-37", "ISA20-40"), 
    r2vector = c(0.988741332517346, 0.980531452763467, 0.964432235118522, 
    0.937490940089255, 0.98112855572719, 0.980546238115913, 0.973612013511355, 
    0.885558795912372, 0.91861080184515, 0.958233166600198, 0.966724069580382, 
    0.963300046169247, 0.916083167400367, 0.973360534778738, 
    0.290922988792887, 0.914477275133392, 0.958303942270144, 
    0.983832363863185, 0.95495240639314, 0.909349413602939, 0.880678748351204, 
    0.921222182947816, 0.857366261637258, 0.789942272510468, 
    0.976639291157428, 0.937966273838989, 0.89181577550386, 0.962433403071652, 
    0.9045988035216, 0.855658682154787, 0.863317772175625, 0.92646629588243, 
    0.757870100658998, 0.968155288932772, 0.958581830892105, 
    0.970387077270498, 0.98921901193924, 0.928377433266779, 0.991145724586611, 
    0.867359270177371, 0.951048188271076, 0.984164095396373, 
    0.918221461581751), Kdvector = c(-0.647927001814099, -0.287019204850277, 
    -0.272711549967834, -0.241923914722991, -0.642489840179406, 
    -0.333657125396755, -0.453554496556026, -0.193422972611941, 
    -0.470750508728843, -0.229594098680724, -0.348730010378211, 
    -0.602945946492937, -0.363990717057327, -0.280640447854594, 
    -0.176725954613923, -0.576220564959256, -0.23362302342339, 
    -0.27803460059131, -0.211602168521757, -0.389060141477508, 
    -0.308652363687686, -0.37695065729472, -0.243209332270608, 
    -0.278407833299843, -0.239972427421248, -0.301232268963952, 
    -0.135687398592413, -0.619070582405233, -0.308507708313094, 
    -0.327508064375853, -0.319486622473713, -0.390285066608756, 
    -0.244244274407136, -0.259551261223599, -0.20948818960757, 
    -0.267423017502296, -0.310923178079829, -0.363404227441907, 
    -0.279673548711697, -0.17859371091819, -0.351995172556264, 
    -0.304832368028169, -0.472146164613578)), class = "data.frame", row.names = c(NA, 
-43L))
> dput(Kd.df)
structure(list(sIDs = c("GBA20-10501", "GBA20-10506", "GBA20-10507", 
"GBA20-10508", "GBA20-10509", "GBA20-10510", "GBA20-10511", "GBA20-10512", 
"GBA20-10513", "GBA20-10516", "GBA20-10517", "GBA20-10518", "GBA20-10519", 
"GBA20-10520", "GBA20-10521", "GBA20-10522", "GBA20-10523", "GBA20-10524", 
"GBA20-10525", "GBA20-10526", "GBA20-10527", "GBA20-10528", "GBA20-10529", 
"GBA20-10530", "GBA20-10531", "GBA20-10532", "GBA20-10533", "GBA20-10534", 
"GBA20-10535", "GBA20-10536", "GBA20-10537", "GBA20-10538", "GBA20-10539", 
"GBA20-10540", "GBA20-10541", "GBA20-10542", "ISA20-13", "ISA20-16", 
"ISA20-23", "ISA20-27", "ISA20-30", "ISA20-37", "ISA20-40"), 
    r2vector = c(0.988741332517346, 0.980531452763467, 0.964432235118522, 
    0.937490940089255, 0.98112855572719, 0.980546238115913, 0.973612013511355, 
    0.885558795912372, 0.91861080184515, 0.958233166600198, 0.966724069580382, 
    0.963300046169247, 0.916083167400367, 0.973360534778738, 
    0.290922988792887, 0.914477275133392, 0.958303942270144, 
    0.983832363863185, 0.95495240639314, 0.909349413602939, 0.880678748351204, 
    0.921222182947816, 0.857366261637258, 0.789942272510468, 
    0.976639291157428, 0.937966273838989, 0.89181577550386, 0.962433403071652, 
    0.9045988035216, 0.855658682154787, 0.863317772175625, 0.92646629588243, 
    0.757870100658998, 0.968155288932772, 0.958581830892105, 
    0.970387077270498, 0.98921901193924, 0.928377433266779, 0.991145724586611, 
    0.867359270177371, 0.951048188271076, 0.984164095396373, 
    0.918221461581751), Kdvector = c(-0.647927001814099, -0.287019204850277, 
    -0.272711549967834, -0.241923914722991, -0.642489840179406, 
    -0.333657125396755, -0.453554496556026, -0.193422972611941, 
    -0.470750508728843, -0.229594098680724, -0.348730010378211, 
    -0.602945946492937, -0.363990717057327, -0.280640447854594, 
    -0.176725954613923, -0.576220564959256, -0.23362302342339, 
    -0.27803460059131, -0.211602168521757, -0.389060141477508, 
    -0.308652363687686, -0.37695065729472, -0.243209332270608, 
    -0.278407833299843, -0.239972427421248, -0.301232268963952, 
    -0.135687398592413, -0.619070582405233, -0.308507708313094, 
    -0.327508064375853, -0.319486622473713, -0.390285066608756, 
    -0.244244274407136, -0.259551261223599, -0.20948818960757, 
    -0.267423017502296, -0.310923178079829, -0.363404227441907, 
    -0.279673548711697, -0.17859371091819, -0.351995172556264, 
    -0.304832368028169, -0.472146164613578)), class = "data.frame", row.names = c(NA, 
-43L))

Thanks so much!

I think you want to do something like this:

library(dplyr)
Kd.df <- Kd.df |> mutate(Kdvector = ifelse(r2vector < 0.7, NA, Kdvector))
1 Like

Awesome, thank you so much! I didn't know about that |> syntax or the ifelse function in dplyr. Thanks again for all your help!

Actually, ifelse is part of base R. It is the mutate function that comes from dplyr. The pipe operator, |>, was added fairly recently. You may well see another version of it, %>%, that is part of the magrittr package and has been available for quite a while.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.