Applying IF statement to entire column in R -


i trying find quick way apply if statement entire column in r create new column of 1's , 0's depending on statement. want calculate days rainfall > 0 wet days (1) , days rainfall =0 dry days (0). data takes form:

day   month   year   rainfall  1       1     1961    2.5  2       1     1961     0  3       1     1961    3.1  4       1     1961     0 

i have created loop follows:

for (i in df$rainfall){  wd=data.frame(ifelse(df$rainfall>0,1,0)  } 

this works, slow. there quicker way?

many thanks.

we can wrap + logical condition coerce binary vector. fast.

+(df$rainfall >0) 

or if using ifelse, there no need loop, vectorized

 ifelse(df$rainfall > 0, 1, 0) 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -