0

I would like to gt average value for the elements on specific position in list. Please could you help me?

I have a list file of elements:

> [[1]] [1] 53 53 53 53 41 53 59 59 51 53 53 53 53 53 53 53 53 36 53 53
> 47 53 53 47 53
> 
> [[2]] [1] 53 53 53 47 53 59 60 59 59 60 60 54 60 59 59 59 45 56 56 55
> 55 41 53
> 
> [[3]] [1] 61 61 64 61 66 64 61 60 60 60 60 59 56 56 56 56 42 53 58 58
> 58 63 52 63 55

And I would like to get vector of numbers with average values:

> [1] 55.67 55.67 ....

Count of element is not same..

Could you help me please? Thank you so much..

Heroka
  • 12,889
  • 1
  • 28
  • 38
Vonton
  • 2,872
  • 4
  • 20
  • 27
  • Have a look at t[his post](http://stackoverflow.com/questions/17308551/do-callrbind-list-for-uneven-number-of-column?answertab=votes#tab-top) to `rbind` your list elements together. Then use `colMeans`, using the argument `na.rm=TRUE` – user20650 Sep 23 '15 at 23:46

1 Answers1

1

You should use the sapplyfunction:

sapply(yourList,mean)
Antares42
  • 1,406
  • 1
  • 15
  • 45
  • Yess, I tried it before you rewrite answer :-) but thank you a lot, i will study this function lapply and apply too... Thank you – Vonton Sep 23 '15 at 08:41
  • Ouch but I think about it now and the result is not so good for me, I would like to get max length from [[1]] or [[2]], but now I get length of all [[1]]+[[2]] etc... :-( – Vonton Sep 23 '15 at 08:56
  • I don't quite understand your desired result. But note that you can use other functions to apply. ``sapply(yourList,max)`` for example will give you the maximum of each list element, i.e. a vector containing (max[[1]], max[[2]], ...). – Antares42 Sep 23 '15 at 09:34
  • No I do not want to use other function... but in the list first vector has length 10 , second vector 9 and third 11 elements and I would like to get get vector with length 11 (max from them) and for each element average. No I get legnth of vector 30 (first+second+third) – Vonton Sep 23 '15 at 09:38
  • You're not explaining this very well. ``sapply(yourList,mean)`` will not return a vector with 30 elements if your list only has 3 elements. Do you want to *select* the list element containing the longest vector (which would be the third in your example)? In that case you could get the index of the longest element by running ``which.max(sapply(yourList,length))``. Really: What do you mean by "you want to get the vector with length 11 and for each element average"? – Antares42 Sep 23 '15 at 10:08
  • simple: [[1]] [1] 2 3 , [[2]] [1] 2 2 3, [[3]] [1] 5 5 7 and I would like to get [1] (2+2+5)/3 (3+2+5)/3 (0+3+7)/3 => [1] 3 3.33 3.33 – Vonton Sep 23 '15 at 10:18
  • 1
    I see. Basically you want to turn your list of vectors of unequal length into a matrix, filling in zeros for missing values (in the shorter rows). Then you want to get a vector of column averages. I'm a bit worried that you're doing something that's not strictly statistically meaningful (which is also why R doesn't have a ready-made solution for it), but give me a moment, I'll get back to you. – Antares42 Sep 24 '15 at 08:34
  • 1
    OK, let's see: You can turn your list of vectors into a matrix using this command: ``yourMatrix <- t(sapply(yourList, '[', 1:max(sapply(yourList, length))))``. This will generate ``NA`` for the shorter rows, which you can set to zero like this: ``yourMatrix[is.na(yourMatrix)] <- 0``. At last you can get the column-wise means by simply calling ``colMeans(yourMatrix)``. But again, beware. I don't know what sort of data you're working with, so I don't know how meaningful it is to calculate an average across potentially missing elements. – Antares42 Sep 24 '15 at 08:42
  • it works really great. Thank you for your time and professional help – Vonton Sep 24 '15 at 14:24