Skip to content
Prev 66607 / 398502 Next

Function Arguments

On Sat, 2005-03-26 at 15:43 -0500, Doran, Harold wrote:
Harold,

Perhaps I am being confused by your example code, which can all be
replaced by:

  tapply(data$dv, list(data$idvar), mean)

Using the 'warpbreaks' data in ?tapply, get the mean of 'breaks' for
each level of 'tension':
L        M        H
36.38889 26.38889 21.66667


Of course, 'mean' can be replaced by more a more complex function call
and additional arguments.


Or you can use by():
INDICES: L
[1] 36.38889
------------------------------------------------------ 
INDICES: M
[1] 26.38889
------------------------------------------------------ 
INDICES: H
[1] 21.66667


or you can use split() on the data frame first, followed by sapply():

# split warpbreaks into a list of 3 data frames by the value of 
# tension, each containing only 'breaks'
# now use sapply to get the mean of breaks in each df:
L        M        H 
36.38889 26.38889 21.66667


Or even:
Tension        x
1       L 36.38889
2       M 26.38889
3       H 21.66667


However, presuming that your actual code is rather different and the key
is that you are really having problems referencing the column elements
in your data frame, the line:

  result[i]<-mean(get("tmp1")[[dv]])

would require that you pass the argument 'dv' as a character variable in
the original function call, such as:

  foobar.fun(..., ..., dv = "VectorName")

When extracting a data frame column or list element using '[' or '[[',
the index(s) value must be either numeric or character.

So, again using the warpbreaks data to get the breaks column:
[1] 26 30 54 25 70 52 51 26 67 18 21 29 17 12 18 35 30 36 36 21 24 18
[23] 10 43 28 15 26 27 14 29 19 29 31 41 20 44 42 26 19 16 39 28 21 39
[45] 29 20 21 24 17 13 15 15 16 28
[1] 26 30 54 25 70 52 51 26 67 18 21 29 17 12 18 35 30 36 36 21 24 18
[23] 10 43 28 15 26 27 14 29 19 29 31 41 20 44 42 26 19 16 39 28 21 39
[45] 29 20 21 24 17 13 15 15 16 28
[1] 26 30 54 25 70 52 51 26 67 18 21 29 17 12 18 35 30 36 36 21 24 18
[23] 10 43 28 15 26 27 14 29 19 29 31 41 20 44 42 26 19 16 39 28 21 39
[45] 29 20 21 24 17 13 15 15 16 28
[1] 26 30 54 25 70 52 51 26 67 18 21 29 17 12 18 35 30 36 36 21 24 18
[23] 10 43 28 15 26 27 14 29 19 29 31 41 20 44 42 26 19 16 39 28 21 39
[45] 29 20 21 24 17 13 15 15 16 28

However:
Error in (function(x, i) if (is.matrix(i)) as.matrix(x)[[i]]
else .subset2(x,  : 
	Object "breaks" not found

or
Error in "[.data.frame"(warpbreaks, , breaks) : 
	Object "breaks" not found


HTH,

Marc Schwartz
<Will be away from e-mail for a while)