[R] concatenating factor from list

2006-03-15 Thread Sebastian Luque
Hi,

I've run into a ridiculous problem I can't find any solutions for in the
archives or help pages:

data(barley)
cutYield - with(barley, by(yield, variety, cut, breaks = c(0, 30, 60, 90)))

As in this example, I'm using 'by' to return a factor for each level of
another factor.  The problem is that 'by' returns a list of the factors,
and I need all these factors concatenated.  No problem, I said:

unlist(cutYield)

which returns an 'integer' class object, so it's no longer a factor.  The
same happens with:

do.call(c, cutYield)

I could recreate the factor from the integer level codes returned above,
but this is not good because this means redoing the job already done by
the function called in 'by', making the code more prone to errors.

Thanks in advance for any pointers,

-- 
Sebastian

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] concatenating factor from list

2006-03-15 Thread Gabor Grothendieck
Is this ok or is it what you are trying to avoid:

  factor(unlist(lapply(cutYield, as.character)))


On 3/15/06, Sebastian Luque [EMAIL PROTECTED] wrote:
 Hi,

 I've run into a ridiculous problem I can't find any solutions for in the
 archives or help pages:

 data(barley)
 cutYield - with(barley, by(yield, variety, cut, breaks = c(0, 30, 60, 90)))

 As in this example, I'm using 'by' to return a factor for each level of
 another factor.  The problem is that 'by' returns a list of the factors,
 and I need all these factors concatenated.  No problem, I said:

 unlist(cutYield)

 which returns an 'integer' class object, so it's no longer a factor.  The
 same happens with:

 do.call(c, cutYield)

 I could recreate the factor from the integer level codes returned above,
 but this is not good because this means redoing the job already done by
 the function called in 'by', making the code more prone to errors.

 Thanks in advance for any pointers,

 --
 Sebastian

 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] concatenating factor from list

2006-03-15 Thread Sebastian Luque
Gabor Grothendieck [EMAIL PROTECTED] wrote:

 Is this ok or is it what you are trying to avoid:

 factor(unlist(lapply(cutYield, as.character)))

Thank you Gabor.  The problem with that is what if some levels do not
appear in any member of cutYield?  In that case, the factor created above
would contain fewer levels than those present in every member of cutYield.

Cheers,

-- 
Sebastian P. Luque

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] concatenating factor from list

2006-03-15 Thread Gabor Grothendieck
Since all components of cutYield have the same levels, one
could do this to ensure that all levels are represented:

factor(unlist(lapply(cutYield, as.character)), levels = levels(cutYield[[1]]))


On 3/15/06, Sebastian Luque [EMAIL PROTECTED] wrote:
 Gabor Grothendieck [EMAIL PROTECTED] wrote:

  Is this ok or is it what you are trying to avoid:

  factor(unlist(lapply(cutYield, as.character)))

 Thank you Gabor.  The problem with that is what if some levels do not
 appear in any member of cutYield?  In that case, the factor created above
 would contain fewer levels than those present in every member of cutYield.

 Cheers,

 --
 Sebastian P. Luque

 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] concatenating factor from list

2006-03-15 Thread Sebastian Luque
Sebastian Luque [EMAIL PROTECTED] wrote:

Gabor Grothendieck [EMAIL PROTECTED] wrote:

 Is this ok or is it what you are trying to avoid:

 factor(unlist(lapply(cutYield, as.character)))

 Thank you Gabor.  The problem with that is what if some levels do not
 appear in any member of cutYield?

This addition to your code takes care of that, although it's a bit
expensive:

factor(unlist(lapply(cutYield, as.character)),
   levels = unique(unlist(lapply(test, levels


Thanks!

-- 
Sebastian P. Luque

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] concatenating factor from list

2006-03-15 Thread Gabor Grothendieck
On 3/15/06, Sebastian Luque [EMAIL PROTECTED] wrote:
 Sebastian Luque [EMAIL PROTECTED] wrote:

 Gabor Grothendieck [EMAIL PROTECTED] wrote:

  Is this ok or is it what you are trying to avoid:

  factor(unlist(lapply(cutYield, as.character)))

  Thank you Gabor.  The problem with that is what if some levels do not
  appear in any member of cutYield?

 This addition to your code takes care of that, although it's a bit
 expensive:

 factor(unlist(lapply(cutYield, as.character)),
   levels = unique(unlist(lapply(test, levels



In thinking about this a bit more here is another solution which
does not disassemble and reassemble the factors and so may
be more along the lines you were looking for:

do.call(rbind, lapply(cutYield, function(x) data.frame(x = x)))$x

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html