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

RNoob ae at alpha-centauri.com
Wed Jan 18 09:45:50 CET 2012


as.matrix()  will not help here. I will get the same error message.

And also, I don't need correlation matrices. I simply need a vector of
correlations. I will show you some code and data I am using.

Here you can see my main dataframe:

> head(test)

     industry       date    testvar       retf1
1 Industrials 2004-12-31 1174382477 -0.02240908
2 Industrials 2005-01-31 1101039133  0.08080221
3 Industrials 2005-02-28 1211723486  0.05646877
4 Industrials 2005-03-31 1253428861 -0.05743186
5 Industrials 2005-04-30 1152846793 -0.02928415
6 Industrials 2005-05-31 1070386589  0.05865941

Now I want to take column "industry" or "date" or both - whatever - as
grouping columns for correlations between testvar and retf1 as follows:

*> numbers <- test[,c("testvar","retf1")]*

> head(numbers)
     testvar       retf1
1 1174382477 -0.02240908
2 1101039133  0.08080221
3 1211723486  0.05646877
4 1253428861 -0.05743186
5 1152846793 -0.02928415
6 1070386589  0.05865941


*> groups <- test[,"industry"]*

> head(groups)
[1] "Industrials" "Industrials" "Industrials" "Industrials" "Industrials"
[6] "Industrials"

> head(unique(groups))
[1] "Industrials"        "Telecommunications" "Financials"        
[4] "Utilities"          "ConsumerGoods"      "OilandGas"

*AND NOW:*

*> aggregate(numbers,by=list(groups),FUN="cor")*

Fehler in FUN(X[[1L]], ...) : supply both 'x' and 'y' or a matrix-like 'x'

So my desired output is a vector of correlations between subsets of x =
"testvar"  and y = "retf1". The length of the resulting vector will be
length(unique(groups)).

I think this draws a clearer picture to you. Sorry for not precisely
pointing it out in my first post.

Thanks and Regards!








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



More information about the R-help mailing list