lundi 1 juin 2015

ifelse to combine and convert messy month/year data over two columns in R

I'm working with a dataframe that has very messy age data for children. Some is in months, some in years. I need a new column with only age in months. Looks like this

Months Yrs
18    1
NA    16    
NA    6 
NA    4 
0    16 
0    17 
0    16 
5    7  
0    8  
0    10 
0    16 
11   0  

Basically I want an ifelse that creates a new column, saying if $Month is a number, print that, but if $Month is NA or 0, print $Year*12.

I tried the following:

child$agemonth<-ifelse(child$G4_R_2_mth == '0 | NA', child$G4_R_2_yr*12, child$G4_R_2_mth)

But got this error:

Error in `$<-.data.frame`(`*tmp*`, "agemonth", value = logical(0)) : 
replacement has 0 rows, data has 1102

Maybe my error is simple but I appreciate the help. One of a ton of problems and driving me crazy. Thanks!

Aucun commentaire:

Enregistrer un commentaire