Skip to content

newbie lapply question

4 messages · Brian Ripley, Ranjan Bagchi, Domenico Vistocco

#
Hi --

I just noticed the following (R 2.6.1 on OSX)
[[1]]
[1] 13514

This is a bit surprising.. Why does lapply unclass the object?  Sorry for 
such a basic question, I don't seem able to produce the right google keywords.

Ranjan
#
On Wed, 5 Dec 2007, Ranjan Bagchi wrote:

            
Did you not read the help page?:

Arguments:

        X: a vector (atomic or list) or an expressions vector.  Other
           objects (including classed objects) will be coerced by
           'as.list'.

and
[[1]]
[1] 13514

BTW, the c() is redundant here: you are concatenating one item only.

As to why as.list() removes the class, read its help page which tells you.

Perhaps if you told us what you are trying to achieve we might be able to 
help you achieve it.
#
On Wed, 5 Dec 2007, Prof Brian Ripley wrote:
Thanks I'll read it more carefully.
I have a function which takes a date as an argument.  I've tested it, and 
I'd like to run it over a range of dates.  So I'm looking at apply- or 
map- type functions.
#
I am not sure to understand your problem, but it seems to me that you 
can use directly the function on the range of the dates:

 > x=as.Date(c('2007-01-01','2007-01-02'))
 > fff=function(x){y=x+1;return(y)}
 > fff(x)
[1] "2007-01-02" "2007-01-03"
 > class(fff(x))
[1] "Date"

Perhaps your function use a different input (not a vector of dates but a 
dataframe)?

domenico vistocco
Ranjan Bagchi wrote: