Examples for 'reshape::sparseby'


Apply a Function to a Data Frame split by levels of indices

Aliases: sparseby

Keywords: iteration category

### ** Examples

x <- data.frame(index=c(rep(1,4),rep(2,3)),value=c(1:7))
x
  index value
1     1     1
2     1     2
3     1     3
4     1     4
5     2     5
6     2     6
7     2     7
sparseby(x,x$index,nrow)
Warning in all(lapply(result, function(x) length(dim(x)) == 2)): coercing
argument of type 'list' to logical
Warning in all(lapply(list(...), is.numeric)): coercing argument of type 'list'
to logical
  x$index  
1       1 4
2       2 3
# The version below works entirely in matrices
x <- as.matrix(x)
sparseby(x,list(group = x[,"index"]), function(subset) c(mean=mean(subset[,2])))
Warning in all(lapply(list(...), is.numeric)): coercing argument of type 'list'
to logical
Warning in all(lapply(result, function(x) length(dim(x)) == 2)): coercing
argument of type 'list' to logical
Warning in all(lapply(list(...), is.numeric)): coercing argument of type 'list'
to logical
  group mean
1     1  2.5
2     2  6.0

[Package reshape version 0.8.9 Index]