[R] Strange data frame behavior

PIKAL Petr petr.pikal at precheza.cz
Tue Sep 25 08:50:54 CEST 2012


Hi

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Raoni Rodrigues
> Sent: Tuesday, September 25, 2012 7:22 AM
> To: r-help at r-project.org
> Subject: [R] Strange data frame behavior
> 
> Hello all,
> 
> I don't understand a strange behavior in data frame manipulation.
> 
> data_frame1 = data.frame(Site = c("S1", "S2", "S3", "S4", "L1", "L2",
> "L3", "L4"),
> 	     				Number = c(1, 3, 5, 2, 1, 1, 2, 1))
> 
> data_frame2 = data_frame1 [data_frame1$Site != "S1", , drop=T]
> 
> dput (data_frame2)
> 
> structure(list(Site = structure(c(6L, 7L, 8L, 1L, 2L, 3L, 4L), .Label =
> c("L1", "L2", "L3", "L4", "S1", "S2", "S3", "S4"), class = "factor"),
>     Number = structure(c(3L, 4L, 2L, 1L, 1L, 2L, 1L), .Label = c("1",
>     "2", "3", "5"), class = "factor")), .Names = c("Site", "Number"
> ), row.names = 2:8, class = "data.frame")
> 
> Why site "S1" do not disappeared from data_frame2's structure?

Because Site is a factor and its levels are preserved in subset operations.

See ?"[" and especially factor part and drop parameter.

You can either get rid of factor and change it to character or explicitly call factor to Site variable

factor(data_frame2$Site)

to get rid of empty levels

Regards
Petr


> 
> And what I have to do to eliminate it definitively from my new data
> frame (data_frame2)?
> 
> Sorry for this basic question, but I really did not understand...
> 
> Thanks in advanced,
> 
> Raoni
> --
> Raoni Rosa Rodrigues
> Research Associate of Fish Transposition Center CTPeixes Universidade
> Federal de Minas Gerais - UFMG Brasil rodrigues.raoni at gmail.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.




More information about the R-help mailing list