Title: sapply with multiple arguments in r
Tags: r
Question: I want to compute a weighted mean over factors with the code below

```factor <- factor(cut(var1, quantile(var1, seq(0,1,0.1))))
var2_split = split(vat2, factor)
weight_split = split(weight, factor)
sapply(var2_split, weighted.mean, weight_split)
```

I get the following error

```Error in FUN(X[[1L]], ...) : 'x' and 'w' must have the same length
```

How do I format my vector and weights for sapply?

As an example

suppose I have a matrix m with 3 columns x,y,z where x is a set of target values, y is a set of weights, and z is a set of values over which I want to bucket weighted.mean(x,y). Specifically I want weighted.mean(x,y) bucketed by quartiles of z.  

```# Code that doesn't work 

x <- c(1,2,3,4,5,6)
y <- c(6,3,4,2,3,4)
z <- c(1,1,2,3,3,4)
m <- as.matrix(c(x,y,z),nrow=6,ncol=3)) 
# bucket z by quartile.
z.factor <- cut(z, quantile(z, seq(0,1,0.25)), include.lowest=TRUE)
x.split = split(x, z.factor)
y.split = split(y, z.factor)
# want to bucket weighted.mean(x,y) on quartiles of z
sapply(x.split, weighted.mean, y.split)
```

Comment: Does mapply work for the example above ?

Comment: Sorry, above is an attempt at what I want to do. I have two problems : (1) grouping by quartiles and (2) applying weighted.mean on each group.

Comment: You can only sapply over one vector/list at a time. If you want to simultaneously iterate allong var2_split and weight_split, try `mapply` or `Map` instead. It would be easier to give a more specific answer if you provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with your question.

Comment: Your example above isn't [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Provide some sample input data (ie `var1`, `vat2`, `weight`, etc) so it's possible to run and test on data that looks like your actual input.

Here is the accepted answer: With your specific sample, try

```#first, note the include.lowest=TRUE to get all values
z.factor <- factor(cut(z, quantile(z, seq(0,1,0.25)), include.lowest=TRUE))

#same
x.split = split(x, z.factor)
y.split = split(y, z.factor)

# here we use mapply
mapply(weighted.mean, x.split, y.split)
```

this gives

```[1,1.25] (1.25,2.5]    (2.5,3]      (3,4] 
1.333333   3.000000   4.600000   6.000000 
```

which seems correct given your sample input.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:09/02/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<r>', 'Title': 'sapply with multiple arguments in r', 'ViewCount': 510, 'id': 25630453, 'user_AboutMe': '', 'user_AccountId': 2344355.0, 'user_DisplayName': 'user196711', 'user_Reputation': 191.0, 'user_Views': 29.0, 'user_id': 2646845.0}}