[R-sig-eco] subsetting data in R

Christian Parker cparker at pdx.edu
Sun Apr 24 15:42:22 CEST 2011


You are creating a new object, but the columns that are stored as factors are not being 'refactored' so you are retaining the original list of levels. To fix this you can use the factor function after you subset

pa2 = subset(pa, influencia=="AID")
pa2$influencia<-as.factor(pa2$influencia)



On Apr 24, 2011, at 6:04 AM, Manuel Spínola <mspinola10 at gmail.com> wrote:

> Dear list members,
> 
> I have a question regarding too subsetting a data set in R.
> 
> I created an object for my data:
> 
>> pa = read.csv("espec_indic.csv", header = T, sep=",", check.names = F)
> 
>> levels(pa$influencia)
> [1] "AID" "AII" "AP"
> 
> The object has 3 levels for influencia (AP, AID, AII)
> 
> Now I subset only observations with influencia = "AID"
> 
>> pa2 = subset(pa, influencia=="AID")
> 
> but if I ask for the levels of influencia still show me the 3 levels, 
> AP, AID, AII.
> 
>> levels(pa2$influencia)
> [1] "AID" "AII" "AP"
> 
> Why is that?
> 
> I was thinking that I was creating a new data frame with only AID as a 
> level for influencia.
> 
> How can I make a complete new object with only the observations for 
> "AID" and that the only level for influencia is indeed "AID"?
> 
> Best,
> 
> Manuel
> 
> 
> 
> 
> -- 
> *Manuel Spínola, Ph.D.*
> Instituto Internacional en Conservación y Manejo de Vida Silvestre
> Universidad Nacional
> Apartado 1350-3000
> Heredia
> COSTA RICA
> mspinola at una.ac.cr
> mspinola10 at gmail.com
> Teléfono: (506) 2277-3598
> Fax: (506) 2237-7036
> Personal website: Lobito de río 
> <https://sites.google.com/site/lobitoderio/>
> Institutional website: ICOMVIS <http://www.icomvis.una.ac.cr/>
> 
>    [[alternative HTML version deleted]]
> 
> _______________________________________________
> R-sig-ecology mailing list
> R-sig-ecology at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology



More information about the R-sig-ecology mailing list