0

Sorry if this has been asked before I have looked throughly but couldn't find anything. My problem is the following. I have survey data and want to perform the same 3 steps with different variables. I always want the output to be separated by gender. So I want to create a function that automates this and returns three new variables. It should look something like this:

myfunction <- function(x) {
       x.mean.by.gender <- svyby(~x, ~gender, svymean, design = s.design)
       x.boxplot <- svyboxplot(x~gender, varwith=FALSE, design = s.design)
       x.ttest <- svyttest(x~gender, design = s.design)}

myfunction(data$income)

This is a working example of what I want to do (boxplot doesn't split by gender but not important):

require("survey")
income <- runif(50, 1000, 2500)
wealth <- runif(50, 10000, 100000)
weight <- runif(50, 1.0, 1.99)
id <- seq(from = 1, to = 50, 1)
gender <- sample(0:1, 50, replace = TRUE)
data <- data.frame(income, wealth, weight, id, gender)
data.w <- svydesign(ids = ~id, data = data, weights = ~weight)
data.w <- update(data.w, count=1)
svytotal(~count, data.w)
# Income difference
income.mean.table <- svyby(~income, ~gender, svymean, design = data.w)
income.mean.table

income.boxplot <- svyboxplot(income~gender, varwith = FALSE, design = data.w)

income.ttest <- svyttest(income~gender, design = data.w)
income.ttest

# Wealth difference
wealth.mean.table <- svyby(~wealth, ~gender, svymean, design = data.w)
wealth.mean.table

wealth.boxplot <- svyboxplot(wealth~gender, varwith = FALSE, design = data.w)

wealth.ttest <- svyttest(wealth~gender, design = data.w)
wealth.ttest

So the function should perform one of those iterations of svyby, svyboxplot and svyttest and create variables with the name of the variable used as input.function (e.g.) income.ttest. I hope this clears things up. Sorry for the confusion.

Daniel Winkler
  • 487
  • 3
  • 11

1 Answers1

0

If data objects are of different type, just make list and return it

UPDATE: version where you could use named elements of the return list

fl <- function(x) {
    a <- x*12.0
    b <- rep(1L, 12)
    c <- "qqqqq"
    list(A = a, B = b, C = c)
}

q <- fl(c(1, 2, 3, 4, 5))

print(q$A)
print(q$B)
print(q$C)
Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64
  • I get the following error when I do that: Error in tapply(1:NROW(x), list(factor(strata)), function(index) { (from #2) : arguments must have same length In addition: Warning messages: 1: In x * pweights : longer object length is not a multiple of shorter object length 2: In x * pweights : longer object length is not a multiple of shorter object length – Daniel Winkler Mar 26 '16 at 19:00
  • @DanielWinkler I've updated the example, made it even simpler, it works for me on Win10, x64, Microsoft **R** 3.2.3 – Severin Pappadeux Mar 26 '16 at 19:32
  • @DanielWinkler It returns list of three different by type/length objects: numeric vector, integer vector and a string. Made access named, should work like data frame – Severin Pappadeux Mar 26 '16 at 19:34
  • Your example works for me too. Unfortunately it doesn't solve the problem. I think the problem is that I want the function input to be a list of values. I think I did not specify the question correctly. I will update that. – Daniel Winkler Mar 26 '16 at 19:38
  • @DanielWinkler you have to provide more info then, some extract of the input data and what do you want but maybe not as a function, just a code – Severin Pappadeux Mar 26 '16 at 19:50
  • The input data is a couple of numeric lists and a binary one for gender. I want to take one of the numeric ones as input for the function and perform the three calculations in the function on them. Now I'm basically doing this: income.mean <- svyby(~income, ~gender, svymean, design = s.design) and then income.boxplot <- svyboxplot(income ~gender, varwith=FALSE, design = s.design) and so on. I have more variables like income for which i would like to perform the same calculations. – Daniel Winkler Mar 26 '16 at 22:14
  • @DanielWinkler please make some reproducible example, otherwise it is hard to follow. Start with something like `dput(head(df, 10))`, then working code. I would refer you to http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Severin Pappadeux Mar 26 '16 at 22:33
  • I added an example. Thank you so much for your help and sorry for not being more clear! – Daniel Winkler Mar 27 '16 at 08:39