[R] Using Aggregate() with FUN arguments, which require more than one input variables

Rui Barradas ruipbarradas at sapo.pt
Tue Jan 17 20:51:47 CET 2012


Hello,


RNoob wrote
> 
> Dear all,
> 
> I am trying to apply the aggregate() function to calculate correlations
> for subsets of a dataframe. My argument x is supposed to consist of 2
> numerical vectors, which represent x and y for the cor() function. 
> 
> The following error results when calling the aggregate function: Error in
> FUN(X[[1L]], ...) : supply both 'x' and 'y' or a matrix-like 'x'. I think
> the subsets aggregate puts into cor() are sort of list types and therefore
> can't be handled by cor().
> 
> Can anyone provide me with a solution?
> 
> Regards,
> RNoob
> 

I don't know if I'm understanding it well but it seems you're trying to
compute a correlation matrix for each group of a data.frame. The data.frame
is divided into groups by one or more factor columns.  If this is what you
want, try the function below. It doesn't use 'aggregate', it uses 'split'
and 'lapply'.


cor.groups <- function(x, vars){
	cols <- if(is.character(vars)) names(x) else 1:ncol(x)
	cols <- cols %in% vars
	cols <- cols | sapply(x, is.factor) | sapply(x, is.character)
	# transform logical to numeric index
	cols <- which(cols)

	lapply(split(x, x[, vars]), function(grp) cor(grp[, -cols]))
}

# Sample data
N <- 100
DF <- data.frame(U=as.factor(sample(LETTERS[1:3], N, T)),
			V=as.factor(sample(0:1, N, T)),
			W=sample(letters[1:6], N, T),
			x=1:N, y=sample(10, N, T), z=rnorm(N),
			stringsAsFactors=FALSE)

# And test it. Note the argument 'stringsAsFactors'

cor.groups(DF, "U")
cor.groups(DF, c("U", "V"))
cor.groups(DF, 1:3)
cor.groups(DF, c("U", "x"))     # look out, right result, wrong function
call


I hope it helps. (if not, be more explicit)

Rui Barradas


--
View this message in context: http://r.789695.n4.nabble.com/Using-Aggregate-with-FUN-arguments-which-require-more-than-one-input-variables-tp4303936p4304535.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list