[R] Vectors out of lists?

David Winsemius dwinsemius at comcast.net
Wed Nov 17 00:52:14 CET 2010


On Nov 16, 2010, at 6:18 PM, Phil Spector wrote:

> Eduardo -
>   I'd guess that
>
> Ybar = function(u)mean(sapply(Y,function(fun)fun(u)))

I had an example to which this was offered and all it did was make  
Ybar a function;

# Polymorphic Fn-object Version 1
Fs <- list(mode="language")
 > Fs
$mode
[1] "language"

 > Fs[[1]] <- mean
 > Fs[[1]](1:10)
[1] 5.5
 > Fs[[2]] <- exp
 > Fs[[3]] <- log

 > Ybar()
[1] NA
Warning message:
In mean.default(sapply(Fs, function(fun) fun(u))) :
   argument is not numeric or logical: returning NA

Trying to offer it an argument u=1:10:
 > Ybar(u)
[1] NA
Warning message:
In mean.default(sapply(Fs, function(fun) fun(u))) :
   argument is not numeric or logical: returning NA

Dallazuanna's suggestion threw an error with that object, and also  
returned NA with this object:

# Polymorphic Fn-objectVersion 2
 > Fs <- vector(mode="list", length=0)

 > Fs[[1]] <- mean
 > Fs[[1]](1:10)
[1] 5.5
 > Fs[[2]] <- exp
 > Fs[[3]] <- log

So I really don't know what theOP's object looks like but the two that  
I made don't seem to be working to well.
>
> will do what you want, but without a reproducible example,it's hard  
> to tell.

Right. What he said.
-- 
David.

> 					- Phil Spector
> 					 Statistical Computing Facility
> 					 Department of Statistics
> 					 UC Berkeley
> 					 spector at stat.berkeley.edu
>
>
> On Tue, 16 Nov 2010, Eduardo de Oliveira Horta wrote:
>
>> Hello there
>>
>> I have a list, Y, and each component of that list is a real-valued  
>> function
>> (that is, Y[[i]](u) returns a number).
>>
>> I was wishing to build the mean function and the first thing I  
>> thought of
>> was
>>
>> Ybar<-function(u){
>>  mean(Y[[1:n]](u))
>> }
>>
>> but obviously this doesn't work, since Y[[1:n]] is not allowed at  
>> all.
>>
>> Any elegant suggestions? I really would like to avoid something like
>>
>> Ybar<-function(u){
>>  for (i in 1:n){
>>     Ybar<-Ybar + Ybar[[i]](u)
>>  }
>>  Ybar<-1/n*Ybar
>> }
>>
>> Thanks in advance!
>> Best regards,
>>
>> Eduardo Horta
>>
>> 	[[alternative HTML version deleted]]


David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list