[R] Average of results coming from B=100 repetitions (looping)

Daniel Nordlund djnord|und @end|ng |rom gm@||@com
Tue May 8 21:58:35 CEST 2018


On 5/8/2018 12:26 PM, varin sacha via R-help wrote:
> 
>   Dear R-experts,
> 
> Here below the reproducible example. I am trying to get the average of the 100 results coming from the "lst" function. I have tried lst$mean and mean(lst). It does not work.
> Any help would be highly appreciated >
> ####################
> 
>   ## R script for getting MedAe and MedAeSQ from HBR model on Testing data
> install.packages("robustbase")
> install.packages( "MASS" )
> install.packages( "quantreg" )
> install.packages( "RobPer")
> install.packages("devtools")
> library("devtools")
> install_github("kloke/hbrfit")
> install.packages('http://www.stat.wmich.edu/mckean/Stat666/Pkgs/npsmReg2_0.1.1.tar.gz')
> library(robustbase)
> library(MASS)
> library(quantreg)
> library(RobPer)
> library(hbrfit)
> 
> # numeric variables
> A=c(2,3,4,3,2,6,5,6,4,3,5,55,6,5,4,5,6,6,7,52)
> B=c(45,43,23,47,65,21,12,7,18,29,56,45,34,23,12,65,4,34,54,23)
> D=c(21,54,34,12,4,56,74,3,12,71,14,15,63,34,35,23,24,21,69,32)
> 
> # Create a dataframe
> BIO<-data.frame(A,B,D)
> 
> # Create a list to store the results
> lst<-list()
> 
<<<snip>>>

You need to spend some time with the Introduction to R that came with 
your R installation.  First, lst in your example is not a function, it 
is a list. And as you found, the mean() function does not work on a 
list. Second, your "minimal reproducible" example could have been 
something like this

lst <- list()
for (i in 1:10) lst[i] <- i
mean(lst)  # does not work

The documentation for mean, ?mean, says that it is looking for a numeric 
or logical vector.  To convert your list to a numeric vector you could 
unlist() it.

mean(unlist(lst))


Hope this is helpful,

Dan

-- 
Daniel Nordlund
Port Townsend, WA  USA




More information about the R-help mailing list