RNoob
2012-Jan-17 17:10 UTC
[R] Using Aggregate() with FUN arguments, which require more than one input variables
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 -- View this message in context: http://r.789695.n4.nabble.com/Using-Aggregate-with-FUN-arguments-which-require-more-than-one-input-variables-tp4303936p4303936.html Sent from the R help mailing list archive at Nabble.com.
Uwe Ligges
2012-Jan-17 18:20 UTC
[R] Using Aggregate() with FUN arguments, which require more than one input variables
On 17.01.2012 18:10, 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().as.matrix() will probably help, but since you have not specified your reproducible code, we cannot show how to change that. Uwe Ligges> Can anyone provide me with a solution? > > Regards, > RNoob > > -- > View this message in context: http://r.789695.n4.nabble.com/Using-Aggregate-with-FUN-arguments-which-require-more-than-one-input-variables-tp4303936p4303936.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Rui Barradas
2012-Jan-17 19:51 UTC
[R] Using Aggregate() with FUN arguments, which require more than one input variables
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.