0

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
user10579790
  • 333
  • 1
  • 10
  • 2
    Try `your_vec[is.null(your_vec)] <- 0`. If you have `NA` instead of `NULL`, you can replace `is.null` with `is.na` – Sotos Feb 19 '19 at 13:05
  • I am getting this warning message: `the condition has length > 1 and only the first element will be used` – user10579790 Feb 19 '19 at 13:21
  • 1
    If you want to change only a column in a `data.frame`, you need to: `my_data$var[is.null(my_data$var)] <- 0` – RLave Feb 19 '19 at 13:25
  • Do you have a vector or column in a dataframe? – Sotos Feb 19 '19 at 13:26
  • Please use `dput()` to show us a reproducible example of your problem, and show us the expected output. – RLave Feb 19 '19 at 13:27
  • @Rlave: Since I have used `attach(data)` before in the code, i am not using the `$` reference. @Sotos I have a column in a dataframe. But frankly, I am not sure if a column in a dataframe does not automatically satisfy all the vector properties? Also, I cannot shate the exact data but if there is a null value in the Var4 Column then that should be replaced with 0 is what I am expecting. – user10579790 Feb 19 '19 at 13:32
  • Just FYI: https://stackoverflow.com/questions/10067680/why-is-it-not-advisable-to-use-attach-in-r-and-what-should-i-use-instead – RLave Feb 19 '19 at 13:43
  • `sapply(Var4,is.null)` this part is not clear to me, and I suspect that's the cause of your Error. – RLave Feb 19 '19 at 13:44
  • aahh...you want to replace in all columns of your data? – Sotos Feb 19 '19 at 13:46
  • No, I want to replace only in the Var4 and in the "if" part is a condition, based on which that particular cell's value should be changed. – user10579790 Feb 19 '19 at 13:53
  • Basically, I have two conditions: 1 that the Begin date should be smaller than the Date_new and if the Var4's value is null, in that case it should be replaced by 0. ` if(Begin_date <= Date_new & sapply(Var4,is.null)){Var4=0}`. That's what I was trying here... – user10579790 Feb 19 '19 at 13:55

1 Answers1

0

With the help of the above comments, this solution worked for me:

 Data$Var4Copy<-ifelse(Begin_date <= Date_new & is.na(Data$Var4),Data$Var4Copy[is.na(Data$Var4)] <- 0, Data$Var4Copy<- Data$Var4)
user10579790
  • 333
  • 1
  • 10