jeudi 5 juillet 2018

Infilling values from column in dataframe with conditional statement for NAs - R

I have a dataframe as follows:

Date        FLOW     Modelled   Infilled
01-01-1992  1.856    1.900      NA
02-01-1992  1.523    1.500      NA
03-01-1992  NA       2.400      NA    
04-01-1992  3.679    3.800      NA

I want to fill the Infilled column with FLOW values. Where there are "NA" values in the FLOW column of the time series I want to replace these NAs with values from the Modelled column.

Answer should look like this:

Date        FLOW     Modelled   Infilled
01-01-1992  1.856    1.900      1.856
02-01-1992  1.523    1.500      1.523
03-01-1992  NA       2.400      2.400    
04-01-1992  3.679    3.800      3.679

I have a solution as follows in excel:

Infilled column   =IF((FLOW="NA"),Modelled,FLOW)

I have not yet found a solution online to help me programme this in R. The time series are pretty lengthy and I have multiple files to do this for, so a loop could be the most suitable solution. I am relatively new to R and I can't figure this out. Help much appreciated!

Aucun commentaire:

Enregistrer un commentaire