I want o replace values in two attributes in a dataframe. CheckIn
and CheckOut
. '--' should be replaced by NA where one of these two attribute value is missing. I tried to put some conditional logic and apply functions but failed. finally i tried using for loop.
for(i in length(Empl.A)){
if((Empl.A$CheckIn[i] == '--') & (Empl.A$CheckOut[i] == '--'))
{
Empl.A$CheckIn[i] <- NA
Empl.A$CheckOut[i] <- NA
}
}
please refer me to some useful resource on replacing values Or to solve these kind of problems.
You can achieve this in a vectorized way without resorting to a loop.
> xy <- data.frame(a = 1:3, ci = c("a", "--", "--"), co = c("b", "--", "--"))
> xy
a ci co
1 1 a b
2 2 -- --
3 3 -- --
> xy[xy$ci == "--" & xy$co == "--", c("ci", "co")] <- NA
> xy
a ci co
1 1 a b
2 2 <NA> <NA>
3 3 <NA> <NA>