I am trying to find the null values in a variable and replace it with 0 in case the condition is satisfied. But this code doesnt seem to work.
if(Begin_date <= Date_new & sapply(Var4,is.null)){Var4=0}
Should i be using a for Loop for Var4 and then replace its values? PLease note Var4 has 86500 values and is of type "DBL".
ID Year Month Begin_date Var4 Date_new
<dbl> <dbl> <dbl> <dttm> <dbl> <date>
1 7 2011 1 2008-01-01 00:00:00 2010-01-01
2 7 2011 2 2008-01-01 00:00:00 0 2010-02-01
3 7 2011 3 2008-01-01 00:00:00 0 2010-03-01
4 7 2011 4 2008-01-01 00:00:00 0 2010-04-01
5 7 2011 5 2008-01-01 00:00:00 0 2010-05-01
6 7 2011 6 2008-01-01 00:00:00 0 2010-06-01
7 7 2011 7 2008-01-01 00:00:00 0 2010-07-01
8 7 2011 8 2008-01-01 00:00:00 0 2010-08-01
9 7 2011 9 2008-01-01 00:00:00 2010-09-01
10 7 2011 10 2008-01-01 00:00:00 0 2010-10-01
I also tried this:
if(Begin_date <= Date_new & sapply(Var4,is.null)){
for(i in 1:nrow(Basedata)){Var4[i]<-0}}
But I am getting this error:
the condition has length > 1 and only the first element will be used