[R] apply, lapply and data.frame in R 2.5

jiho jo.irisson at gmail.com
Mon Jul 30 13:54:38 CEST 2007

On 2007-July-30  , at 12:20 , Prof Brian Ripley wrote:
> On Mon, 30 Jul 2007, jiho wrote:
>> A recent (in 2.5 I suspect) change in R is giving me trouble. I want
>> to apply a function (tolower) to all the columns of a data.frame and
>> get a data.frame in return.
>> Currently, on a data.frame, both apply (for arrays) and lapply (for
>> lists) work, but each returns its native class (resp. matrix and  
>> list):
>> apply(mydat,2,tolower) 	# gives a matrix
>> lapply(mydat,tolower)	# gives a list
>> and
>> sapply(mydat,tolower)	# gives a matrix
> which is exactly what R 2.0.0 did, so no recent(ish) change at all.
>> If I remember well, apply did not used to work on data.frames and
>> lapply returned a data.frame when it was provided with one, with the
>> same properties (columns classes etc). At least this is what my code
>> written with R 2.4.* suggests.
> apply has coerced data frames for many years and lapply always  
> returned a list.  The solution has always been
> mydat[] <- lapply(mydat,tolower)

sorry about that, my previous code was misleading and indeed your  
code above does exactly what I need. I should have tested this a bit  
further before posting. I was just afraid to install two different R  
versions I guess.
thank you again.


More information about the R-help mailing list