Hello R user,

I have following data frame:

df=data.frame(id=c(1:10),strata=rep(c(1,2),c(5,5)),y=c(
10,12,10,NA,15,70,NA,NA,55,100),x=c(3,4,5,7,4,10,12,8,3,15))

and I would like to replace NA's with:

instead of first NA  tapply(na.exclude(df)$y,na.exclude(df)$strata,sum)[1]*
*7 */tapply(na.exclude(df)$x,na.exclude(df)$strata,sum)[1]
where 7 is the value of x (id=4) in strata 1 where y=NA

instead of second NA tapply(na.exclude(df)$y,na.exclude(df)$strata,sum)[2]*
*12 */tapply(na.exclude(df)$x,na.exclude(df)$strata,sum)[2]
where 12 is the value of x (id=7) in strata 2 where y=NA

instead of third NA tapply(na.exclude(df)$y,na.exclude(df)$strata,sum)[2]* *
8 */tapply(na.exclude(df)$x,na.exclude(df)$strata,sum)[2]
where 8 is the value of x(id=8) in strata 2 where y=NA.

So, I would like to replace NA inside the stratas on above explained way.

Does anyone know how to do this?

thanks in advance

Andrija

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to