Re: [R] unwanted coercion using apply

From: David Winsemius <>
Date: Sun, 22 May 2011 15:40:09 -0400

On May 22, 2011, at 3:22 PM, Jeroen Ooms wrote:

>> apply() operates on arrays and a data frame will be coerced to a
>> matrix
>> (which requires all elements to be of the same type). This is
>> documented in
>> ?apply.
> Thanks, I was not aware of that. I implicitly assumed there would be a
> specific

  Because there is a constraint in R that all elements of a vector need to be of the same type.

>> This may not be elegant, but why not just use a loop:
> The thing is that were are going to use this operation on a (very)
> large
> scale in a production environment, on datasets for which nrow >>
> ncol. So I
> would like the most efficient solution and if possibly, avoid
> looping over
> the rows.

But `apply` loops over rows, too. No speed advantage is being gained.

You may get better answers if you post a representative example that has types on which you will be operating. There may be vectorizable solutions that can operate column-wise.

> Thanks


David Winsemius, MD
West Hartford, CT

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Sun 22 May 2011 - 19:42:58 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 22 May 2011 - 19:50:09 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive