[R] unwanted coercion using apply

David Winsemius dwinsemius at comcast.net
Sun May 22 21:40:09 CEST 2011


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 apply.data.frame.

  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



More information about the R-help mailing list