[R] Summarize data for MCA (FactoMineR)

David Winsemius dwinsemius at comcast.net
Sat May 3 20:14:02 CEST 2008


"Nelson Castillo" <nelsoneci at gmail.com> wrote in
news:2accc2ff0805021800p734c640dt179cd273a358a635 at mail.gmail.com: 

> On Sun, Apr 27, 2008 at 10:10 AM, David Winsemius
> <dwinsemius at comcast.net> wrote:
>> "Nelson Castillo" <nelsoneci at gmail.com> wrote in
>>  news:2accc2ff0804251655o32686b99j73cf7df37243d08f at mail.gmail.com:
> 
snip
> 
> That was exactly what I needed :-) Thanks a lot. I tried to make the
> list that you have to pass
> as "by" from   colnames(edom)[2:14] :
> 
>  [1] "VB21"     "VB17_NEV" "VB17_LAV" "VB17_EQS" "VB17_CAL"
>  "VB17_DEL" [7] "VB17_LIC" "VB17_HEL" "VB17_AIR" "VB17_VEN"
>  "VB17_TVC" "VB17_PC" 
> [13] "VB17_HMI"
> 
> 
> But I couldn't do it. 

You needed a list, not the character vector which colnames returns. I 
wonder if it would have worked if you had used:

 by= as.list(colnames(edom)[2:14]) 

Using the orignal examples, note the class of the different objects.
> s.wt
  var1 var2 weight
1    A    B      5
2    C    D      2

> colnames(s.wt)[2:3]
[1] "var2"   "weight"
> class(colnames(s.wt)[2:3])
[1] "character"
> as.list(colnames(s.wt)[2:3])
[[1]]
[1] "var2"

[[2]]
[1] "weight"
> class(as.list(colnames(s.wt)[2:3]))
[1] "list"

If you sent an un-named list to "by", you end up with column names: 
Group.1 through Group.13 in the returned dataframe. I think you could 
have then used:

 colnames(edom2)[1:13] <- c("Income",colnames(edom)[3:14]) 

It may not have been worth the extra trouble, but it serves to 
emphasize the need for using the proper class for function arguments.

-- 
David Winsemius


> So, I did the list by hand.
> 
> edom2 = with(edom,aggregate(FACT_EXP_CAL_H,
> by=list(Income=VB21,VB17_NEV=VB17_NEV, VB17_LAV=VB17_LAV,
> VB17_EQS=VB17_EQS, VB17_CAL=VB17_CAL, VB17_DEL=VB17_DEL,
> VB17_LIC=VB17_LIC, VB17_HEL=VB17_HEL, VB17_AIR=VB17_AIR,
> VB17_VEN=VB17_VEN, VB17_TVC=VB17_TVC, VB17_PC=VB17_PC,
> VB17_HMI=VB17_HMI), sum))
> 
>> nrow(edom2)
> 
> [1] 9817
> 
> And the row count matches what I did before with Perl :-)



More information about the R-help mailing list