Re: [R] Error with lapply [addirional clarification needed]

2008-11-22 Thread Gabor Grothendieck
Try str(ss) to see what it really looks like.  You probably want:

fn <- function(i) c(i, i^2)


On Sat, Nov 22, 2008 at 4:54 AM, megh <[EMAIL PROTECTED]> wrote:
>
> I need one more clarification here :
>
> Here I did :
>
> fn <- function(i) return(list(i, i^2))
> ss = sapply(1:4, fn)
>
> Here the object "ss" should be a matrix object :
> is.matrix(ss)
>
> However I feel it lacks some the matrix object properties. For example the
> syntax "min(ss[1,])" generates an error :
> "Error in min(ss[1, ]) : invalid 'type' (list) of argument".
>
> What should be the way out? Am I missing something ?
>
> Regards,
>
>
> baptiste auguie-2 wrote:
>>
>> Hi,
>>
>> you are feeding lapply "i" as an optional argument, which is passed to
>> fn() and causes an error. Just use lapply(1:4, fn), or better yet,
>> sapply,
>>
>>  > fn <- function(i) return(i^2)
>>  > sapply(1:4, fn)
>> [1]  1  4  9 16
>>
>> Hope this helps,
>>
>> baptiste
>>
>>
>> On 20 Nov 2008, at 16:31, megh wrote:
>>
>>>
>>> I have written following codes, with intention to get a list with
>>> values
>>> 1,2,9,16 :
>>>
>>> fn <- function(i) return(i^2)
>>> lapply(1:4, fn, i)
>>>
>>> However I got following error :
>>> Error in FUN(1:4[[1L]], ...) : unused argument(s) (1)
>>>
>>> Can anyone please tell me what will be the correct code here?
>>>
>>> Regards,
>>>
>>>
>>> --
>>> View this message in context:
>>> http://www.nabble.com/Error-with-lapply-tp20605066p20605066.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> __
>>> R-help@r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>
>> _
>>
>> Baptiste Auguié
>>
>> School of Physics
>> University of Exeter
>> Stocker Road,
>> Exeter, Devon,
>> EX4 4QL, UK
>>
>> Phone: +44 1392 264187
>>
>> http://newton.ex.ac.uk/research/emag
>>
>> __
>> R-help@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>
> --
> View this message in context: 
> http://www.nabble.com/Error-with-lapply-tp20605066p20634821.html
> Sent from the R help mailing list archive at Nabble.com.
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Error with lapply [addirional clarification needed]

2008-11-22 Thread Peter Dalgaard

megh wrote:

I need one more clarification here :

Here I did :

fn <- function(i) return(list(i, i^2))
ss = sapply(1:4, fn)

Here the object "ss" should be a matrix object :
is.matrix(ss)

However I feel it lacks some the matrix object properties. For example the
syntax "min(ss[1,])" generates an error :
"Error in min(ss[1, ]) : invalid 'type' (list) of argument".

What should be the way out? Am I missing something ?


ss is a dim'ed list object, because fn returns a list. This appears to 
be a feature, although rarely used. One point is that you can do


> fn <- function(i) return(list(as.character(i), i^2))
> ss <- sapply(1:4, fn)
> ss
 [,1] [,2] [,3] [,4]
[1,] "1"  "2"  "3"  "4"
[2,] 14916
> ss[,1]
[[1]]
[1] "1"

[[2]]
[1] 1

I.e., the rows can be of different mode.

The easiest way out is just not to do that, i.e. return a vectore c(i, 
i^2) instead.



--
   O__   Peter Dalgaard Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark  Ph:  (+45) 35327918
~~ - ([EMAIL PROTECTED])  FAX: (+45) 35327907

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Error with lapply [addirional clarification needed]

2008-11-22 Thread megh

I need one more clarification here :

Here I did :

fn <- function(i) return(list(i, i^2))
ss = sapply(1:4, fn)

Here the object "ss" should be a matrix object :
is.matrix(ss)

However I feel it lacks some the matrix object properties. For example the
syntax "min(ss[1,])" generates an error :
"Error in min(ss[1, ]) : invalid 'type' (list) of argument".

What should be the way out? Am I missing something ?

Regards,


baptiste auguie-2 wrote:
> 
> Hi,
> 
> you are feeding lapply "i" as an optional argument, which is passed to  
> fn() and causes an error. Just use lapply(1:4, fn), or better yet,  
> sapply,
> 
>  > fn <- function(i) return(i^2)
>  > sapply(1:4, fn)
> [1]  1  4  9 16
> 
> Hope this helps,
> 
> baptiste
> 
> 
> On 20 Nov 2008, at 16:31, megh wrote:
> 
>>
>> I have written following codes, with intention to get a list with  
>> values
>> 1,2,9,16 :
>>
>> fn <- function(i) return(i^2)
>> lapply(1:4, fn, i)
>>
>> However I got following error :
>> Error in FUN(1:4[[1L]], ...) : unused argument(s) (1)
>>
>> Can anyone please tell me what will be the correct code here?
>>
>> Regards,
>>
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Error-with-lapply-tp20605066p20605066.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> __
>> R-help@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 
> _
> 
> Baptiste Auguié
> 
> School of Physics
> University of Exeter
> Stocker Road,
> Exeter, Devon,
> EX4 4QL, UK
> 
> Phone: +44 1392 264187
> 
> http://newton.ex.ac.uk/research/emag
> 
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Error-with-lapply-tp20605066p20634821.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.