Reputation: 139
I'm interpolating data, but columns with rows that start or end with NA values will not interpolate. The only solution I've found is to delete those NA rows at the start or end of the column, but this is not ideal because I want to keep those rows.
Example of dataframe (df) inputted:
year value
2000 NA
2001 NA
2002 9
2003 7
2004 NA
2005 6
2006 NA
2007 4
2008 NA
Code snippet:
install.packages("zoo")
library(zoo)
newValue <- na.approx(df$value)
df$Value <- newValue
The desired output:
year value
2000 NA
2001 NA
2002 9
2003 7
2004 6.5
2005 6
2006 5
2007 4
2008 NA
How do you interpolate the data when the first or last row has an NA value, while keeping those rows?
Upvotes: 1
Views: 473
Reputation: 139
Solution via @akrun
newValue <- na.approx(df$value, na.rm = FALSE)
df$Value <- newValue
Upvotes: 3