interpolation - How do I add missing rows AND interpolated value to a data frame in R? -
i have table of data this:
dat <- data.frame( age = c(0,10,20,40,70,100), surv = c(1.0,0.9,0.8,0.6,0.3,0.0) ) i add row every age missing (30,50,60,80,90) , add linear interpolation surv new row. new rows (30,0.7) (80, 0.2) etc.
this example. working larger dataset goes 0 5,000,000 10 (with lots of missing rows) need automation.
thanks help!
make master dataset use approx linearly interpolate:
newdat <- merge(data.frame(age=seq(0,100,10)),dat,all=true) newdat$surv[is.na(newdat$surv)] <- with(newdat, approx(age, surv, xout=age)$y)[is.na(newdat$surv)] newdat # age surv #1 0 1.0 #2 10 0.9 #3 20 0.8 #4 30 0.7 #5 40 0.6 #6 50 0.5 #7 60 0.4 #8 70 0.3 #9 80 0.2 #10 90 0.1 #11 100 0.0
Comments
Post a Comment