Skip to content
Prev 53698 / 63424 Next

Simplify and By Convert Factors To Numeric Values

On Fri, 16 Jun 2017, Dario Strbenac wrote:

            
It *is* described in the help page.

This returns a list of objects and each object class has "factor"

tapply(rep(1:2,2), rep(1:2,2),
   function(x) factor(LETTERS[x], levels = LETTERS))

and this
returns a vector object with no class.
and also states

"If FUN returns a single atomic value for each such cell ... and when 
simplify is TRUE ...  if the return value has a class (e.g., an object of 
class "Date") the class is discarded."

which is what just happened in your example.

Maybe you want:

unlist(tapply(1:3, 1:3, function(x) factor(LETTERS[x],
   levels = LETTERS),simplify=FALSE))

Trying to preserve class worked here in a way you might have 
hoped/expected, but might lead to difficulties in other uses.

HTH,

Chuck