For a loop in R to calculate the average from a list

This post is under cross-confirmation and a message was sent to me here.

I have a named list d

that contains values โ€‹โ€‹in R.

> d
$`2017-07-15:10:09:22`
[1] 3.125 4.375 2.500 0.625 5.000 3.750 1.875 1.250

$`2017-07-15:10:10:04`
[1] 0.625 3.750 3.125 1.875 1.250 4.375 2.500 5.000

$`2017-07-15:11:45:45`
[1] 4.375 3.125 3.750 2.500 5.000 1.875 1.250 0.625

      

I am interested in calculating the average of these points and storing it in a dataframe. So I made a for loop to do this

l2 <- length(d)
for(j in 1:6)
{
      df$Mean[j] <- (d[[1]][j] 
            + d[[2]][j] + d[[3]][j])/l2 

}

      

And the length of the list d

grows every time. I would like to do this loop for

to calculate the average myself. Like this..

l2 <- length(d)   
for(j in 1:6)
       {
              df$Mean[j] <- (d[[1]][j] 
                    + d[[2]][j] + d[[3]][j] + d[[4]][j] + ....
               )/l2 

        }

      

how to loop this to do it automatically? Thank.

+3


source to share


2 answers


As F. F. Maas correctly pointed out, you don't need to get stuck here.

But if I understand your question correctly, you want the average of each point to be above all the list items, not the average of the points inside each list item.

If my guess is correct, this code should do what you want it to do, and it doesn't even need to lapply

:

#create test data and names
d <- lapply(1:3,function(x) runif(6))
names(d) <- sample(LETTERS,length(d))

> d
$V
[1] 0.9369505 0.7825348 0.4549225 0.3807600 0.7169146 0.3608166

$Z
[1] 0.75466094 0.09207062 0.59738221 0.33558258 0.79022386 0.98266940

$G
[1] 0.3441581 0.6696056 0.5544217 0.7422718 0.6682706 0.7989314


#calculate means

res <- colMeans(do.call(rbind,d))

      



You now have funds for each point. You can add them to an existing one data.frame

or create a new one.

#put into df

df <- data.frame(means=res)

#output

> df
      means
1 0.6785898
2 0.5147370
3 0.5355755
4 0.4862048
5 0.7251363
6 0.7141391

      

So, you only need one colMeans(do.call(rbind,d))

which folds your list into a matrix with each column representing a point. After that, I can just use colMeans

to calculate the average.

+3


source


I think this is what you are looking for (example code):

> set.seed(1)
> data<-list(a=rnorm(10),b=rnorm(10),c=rnorm(10),d=rnorm(10))
> data_mean<-rep(0,10)
> data
$a
 [1] -0.6264538  0.1836433 -0.8356286  1.5952808  0.3295078 -0.8204684  0.4874291  0.7383247  0.5757814
[10] -0.3053884

$b
 [1]  1.51178117  0.38984324 -0.62124058 -2.21469989  1.12493092 -0.04493361 -0.01619026  0.94383621
 [9]  0.82122120  0.59390132

$c
 [1]  0.91897737  0.78213630  0.07456498 -1.98935170  0.61982575 -0.05612874 -0.15579551 -1.47075238
 [9] -0.47815006  0.41794156

$d
 [1]  1.35867955 -0.10278773  0.38767161 -0.05380504 -1.37705956 -0.41499456 -0.39428995 -0.05931340
 [9]  1.10002537  0.76317575

> lapply(data,"[",n=4) #access the 4th entry of every list part - only needs to be transformed to numeric vector
$a
[1] 1.595281

$b
[1] -2.2147

$c
[1] -1.989352

$d
[1] -0.05380504

> for(i in 1:length(data[[1]]))(
+   data_mean[i]<-mean(as.numeric(lapply(data,"[",n=i)))
+ )
> data_mean
 [1]  0.79074607  0.31320878 -0.24865815 -0.66564396  0.17430122 -0.33413132 -0.01971167  0.03802378
 [9]  0.50471947  0.36740756

      



Basically, you just need to paste mean(as.numeric(lapply(d,"[",n=j)))

in your for

-loop.

+1


source







All Articles