1

I am trying to use update function on survey.design object. For instance, I want to create a variable that is the mean of 4 other variables, as follows

x1<-runif(3)
x2<-runif(3)
x3<-runif(3)
population=10000
testdf<-data.frame(x1,x2,x3,population)
testsvy<-svydesign(id=~1,weights=c(30,30,30),data=testdf)
testsvy<-update(testsvy,avg=mean(c(x1,x2,x3)))

However this returns a vector of the same number for every person. There must be something wrong. Alternatively I can modify on test$variables, but I don't feel that this is the easiest way...

  • 2
    try to make [your problem reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for people without your local data – Nate Jun 01 '18 at 13:53
  • `testsvy <- update( testsvy , avg = rowMeans( cbind( x1 , x2 , x3 ) , na.rm = TRUE ) )` – Anthony Damico Jun 02 '18 at 08:08

1 Answers1

0

OK I got the answer myself... Hope that it could be simpler since I type the object names three times...

testsvy<-update(testsvy,avg2=rowMeans(testsvy$variables[,c("x1","x2","x3")],na.rm=TRUE))