Ifelse does nothing in R

I am a very beginner R programmer and I am trying to convert old SAS code to R. I need to replace values ​​based on a condition, and if the condition is false, leave them alone. I have searched this and tried many solutions but to no avail. The reason I am doing this is to classify the first instance of the event (in this case, the medics are writing the prescription). if in the first month they wrote the recipe in May of last year, their starting month (recruit) is 5. If it was in June, then 6, and so on. I have been working backwards since June this year and I want to update their start of the month (rookie) if an earlier recipe is found. If no earlier recipe is found, I want to keep number one. This is the code I am using:

newwriters$newwriter=ifelse(newwriters$MTRx_06_30_2017>0,18,NULL)
newwriters$newwriter=ifelse(newwriters$MTRx_05_31_2017>0,17,NULL)
newwriters$newwriter=ifelse(newwriters$MTRx_04_30_2017>0,16,NULL)
newwriters$newwriter=ifelse(newwriters$MTRx_03_31_2017>0,15,NULL)
newwriters$newwriter=ifelse(newwriters$MTRx_02_28_2017>0,14,NULL)
newwriters$newwriter=ifelse(newwriters$MTRx_01_31_2017>0,13,NULL)
newwriters$newwriter=ifelse(newwriters$MTRx_12_31_2016>0,12,NULL)
newwriters$newwriter=ifelse(newwriters$MTRx_11_30_2016>0,11,NULL)
newwriters$newwriter=ifelse(newwriters$MTRx_10_31_2016>0,10,NULL)

      

The problem is that it keeps changing higher values ​​to 0 if it doesn't find a recipe this month. I want him to just leave the values ​​alone. I've also tried all of the following with no success:

newwriters$newwriter=ifelse(newwriters$MTRx_06_30_2017>0,18,newwriters$newwriter)
newwriters$newwriter=ifelse(newwriters$MTRx_06_30_2017>0,18,newwriters[,16])
newwriters$newwriter=ifelse(newwriters$MTRx_06_30_2017>0,18,)

      

As I said, I'm new to writing R code. I'm sure there is a better / faster / more efficient way to do this, but I'm not sure what else to try. Thanks in advance for your help!

+3


source to share


2 answers


I'm surprised that

writers$newwriter=ifelse(newwriters$MTRx_06_30_2017>0,18,newwriters$newwriter)

      

does not work. can there be NULL or NA values ​​in these columns? Then you should do something like:

writers$newwriter=ifelse(newwriters$MTRx_06_30_2017>0 & !is.na(newwriters$MTRx_06_30_2017),18,newwriters$newwriter)

      



The following snippet works as expected:

a = data.frame(b= c(1,NA,3))
a$b = ifelse(a>1,10,a$b)

      

Hope this helps!

+3


source


Better to use if_else

from dplyr package . It has an explicit relationship to NA

which makes it more reliable and also slightly faster.

Quick example:



> library(tidyverse)
> iris2 = iris %>% as_data_frame()
> 
> #add some NA's
> iris2$Sepal.Length[c(1, 5, 8)] = NA
> 
> #print
> iris2
# A tibble: 150 x 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
 1        NA           3.50         1.40       0.200 setosa 
 2         4.90        3.00         1.40       0.200 setosa 
 3         4.70        3.20         1.30       0.200 setosa 
 4         4.60        3.10         1.50       0.200 setosa 
 5        NA           3.60         1.40       0.200 setosa 
 6         5.40        3.90         1.70       0.400 setosa 
 7         4.60        3.40         1.40       0.300 setosa 
 8        NA           3.40         1.50       0.200 setosa 
 9         4.40        2.90         1.40       0.200 setosa 
10         4.90        3.10         1.50       0.100 setosa 
# ... with 140 more rows
> 
> #conditionally change
> iris2$new_var = if_else(iris2$Sepal.Length > 5, true = 100, false = 0, missing = -100)
> 
> iris2$new_var
  [1] -100    0    0    0 -100  100    0 -100    0    0  100    0    0    0  100  100  100  100  100  100  100  100    0  100    0    0    0
 [28]  100  100    0    0  100  100  100    0    0  100    0    0  100    0    0    0    0  100    0  100    0  100    0  100  100  100  100
 [55]  100  100  100    0  100  100    0  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100
 [82]  100  100  100  100  100  100  100  100  100  100  100  100    0  100  100  100  100  100  100  100  100  100  100  100  100    0  100
[109]  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100
[136]  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100

      

So we created a new variable where values ​​above 5 changed to 100, below 5 to 0 and NA

-100.

0


source







All Articles