r - how to deal with missing value in if else statement? -


i have dataframe, mydata, constructed follows:

col1<-c(8.20e+07, 1.75e+08, na, 4.80e+07,         3.40e+07, na, 5.60e+07, 3.00e+06 ) col2<-c(1960,1960,1965,1986,1960         ,1969,1960,1993) col3<-c ( na,2.190,na,na, 5.000, na,           1.700,4.220) mydata<-data.frame(col1,col2,col3)  mydata  #       col1 col2 col3 # 1 8.20e+07 1960   na # 2 1.75e+08 1960 2.19 # 3       na 1965   na # 4 4.80e+07 1986   na # 5 3.40e+07 1960 5.00 # 6       na 1969   na # 7 5.60e+07 1960 1.70 # 8 3.00e+06 1993 4.22 

i want create col4 has values "a", "b" , "c", if col1 smaller 4.00e+07, col4=="a"; if col1 not less 4.00e+07, col4=="b", else col4=="c"

here code:

col4 <-ifelse(col1<4.00e+07, "a",                          ifelse(col1 >=4.00e+07, "b",                                 ifelse(is.na(col1 =4.00e+07), "b",  "c" ))) 

but evaluates to:

# [1] "b" "b" na  "b" "a" na  "b" "a" 

it doesn't change na value in col1 "c".

the outcome should be:

 #  [1] "b" "b" "c"  "b" "a" "c" "b" "a" 

what problem in code? suggestion appreciated!

you have check is.na first, because na < 4.00e+07 results in na. if first argument of ifelse() na, result na well:

ifelse(c(na, true, false), "t", "f") ## [1] na  "t" "f" 

as can see, first vector element result indeed na. if other arguments of ifelse() have special code take care of situation, won't because code never taken account.

for example, checking na first gives desired result:

col4 <- ifelse(is.na(col1), "c",                ifelse(col1 < 4.00e+07, "a","b")) col4 ## [1] "b" "b" "c" "b" "a" "c" "b" "a" 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -