[R] min() return factor class values

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Tue Jan 9 15:47:57 CET 2007


Milton Cezar Ribeiro wrote:
> Hi R-friends
>    
>   I don´t know why the "min()" function below return the min value as factor. When i force the aicc.min using a as.numeric() function, it return a factor index (1,2,..) and not min value as I want. By the way, I included a sessionInfo() at the end of this e-mail.
>   
min() is not doing anything out of the ordinary, but cbind'ing it with
the character vector sp coerces it to character and rbind'ing to a data
frame turns character vectors into factors...

The whole thing looks like it could be a straightforward application of
aggregate().
>    
>   In fact I had the same problem (values as factor) on other part of my script and I noticed that it occour when I use "cbind()". It is real?
>    
>   Any idea? 
>    
>   Kind regards,
>    
>   Miltinho
>    
>   > especies.aicc.min<-data.frame()
>   
>> for (sp in levels(especies.aicc$especie)) 
>>     
> + {
> + sele<-subset(especies.aicc,especie==sp)
> + especies.aicc.min<-rbind(especies.aicc.min,cbind(sp,aicc.min=min(sele$aicc)))
> + }
>   
>> especies.aicc.min
>>     
>                         sp         aicc.min
> 1             Attila.rufus  6.7387056413613
> 2 Automolus.leucophthalmus 125.791300522824
>   
>> class(especies.aicc.min$aicc.min)
>>     
> [1] "factor"
>   
> -----------
>   > sessionInfo()
> R version 2.4.0 (2006-10-03) 
> i386-pc-mingw32 
>   locale:
> LC_COLLATE=English_Jamaica.1252;LC_CTYPE=English_Jamaica.1252;LC_MONETARY=English_Jamaica.1252;LC_NUMERIC=C;LC_TIME=English_Jamaica.1252
>   attached base packages:
> [1] "methods"   "stats"     "graphics"  "grDevices" "utils"     "datasets"  "base"     
>   
>
>   


-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list