[R-sig-eco] omit levels from boxplots

Peter Solymos solymos at ualberta.ca
Thu Jan 6 18:36:21 CET 2011


Hi,

for one factor, it is enough to do

dat$ageclass <- dat$ageclass[drop=TRUE]

the 'purgef' function applies the drop statement for each columns in
the data frame and eventually returns a list because that's how
'lapply' works. If you want a data frame in the end, you can either do
(as I recall lists also work supplied as data argument, but just to be
consistent)

dat[] <- lapply(dat, function(x) x[drop=TRUE])

or modify the function as

purgef2 <- function(x) as.data.frame(lapply(x, function(x) x[drop = TRUE]))

Let's see for example:

dat <- data.frame(ageclass=factor(1:2, levels=1:4), sex=factor("male",
levels="male"))
purgef <- function(x) lapply(x, function(x) x[drop = TRUE])
purgef(dat)

$ageclass
[1] 1 2
Levels: 1 2

$sex
[1] male male
Levels: male

purgef2(dat)

  ageclass  sex
1        1 male
2        2 male

But maybe Ben's update is just enough (it came in as I was writing this reply).

Cheers,

Peter



On Thu, Jan 6, 2011 at 10:16 AM, Jarrett Byrnes <byrnes at nceas.ucsb.edu> wrote:
> Ah, so, you have factors that are no longer present in the data.  I use a quick function I wrote called purgef to get rid of zombie factors.
>
> purgef<-function(x) lapply(x, function(x) x[drop = TRUE])
>
>
> So, here, before your boxplot
>
> dat$ageclass <- purgef(dat$ageclass)
>
> On Jan 6, 2011, at 9:09 AM, Howe, Eric (MNR) wrote:
>
>> Good day list members,
>>
>>
>>
>> I was wondering if anyone knows a way to omit specific columns or anova
>> cells from boxplots.
>>
>>
>>
>> E.g. I produce a boxplot as:
>>
>>
>>
>>> boxplot(y ~ ageclass + sex,data=dat)
>>
>>
>>
>> where ageclass has 4 levels (including 1-year-old), and sex has 2
>> levels, but the only males included in the analysis are one-year-olds.
>>
>> The resulting boxplot illustrates differences among age classes of
>> females, and between male and female one-year-olds.
>>
>> However, it has 3 blank columns, corresponding to subadult, young adult,
>> and mature age classes of males.
>>
>> I'd like to exclude those blank columns from the figure.
>>
>>
>>
>> Thanks in advance,
>>
>>
>>
>> Eric
>>
>>
>>
>>
>>
>>
>>       [[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
>
> _______________________________________________
> 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