[R] How to take the average of multiple rows

From: ss <affysnp_at_gmail.com>
Date: Sun, 15 Jun 2008 14:18:55 -0400

Dear all,

I have a matrix, called newdata1,

> dim(newdata1)

[1] 34176 83

It looks like:

            EntrezID    Name      S1  S2   S3    S4 S5.....
1           4076       CAPRIN1  0.1  0.2   0.3...
2           139170    WDR40B  0.4  0.5   0.6...
3            5505    PPP1R2P1  0.3  0.3   0.7...
4            4076       CAPRIN1  0.7  0.3   0.2...
5           139170    WDR40B  null  0.8   0.4...
6           139170    WDR40B  null  null   0.75...

If there are rows whose "EntrezID" and "Name" are exactly the same, I want to take the average for these rows.

There might be some 'null's in the data set. For example, there are three rows of the same "EntrezID" and "Name" as 139170 and WDR40B. For the sample called 'S1', there are three values, 0.4, null, null. For this
scenario, I want to keep the final value as 0.4 For the sample 'S2', the values
are 0.5, 0.8, null. For this, I want to ignore 'null', and take the average of
0.5 and 0.8, the final value is (0.5+0.8)/2=0.65. For the sample 'S3', there is no 'null', so just take the average as (0.6+0.4+0.75)/3=0.5833.

Can you show me how to do this?

I appreciate!



        [[alternative HTML version deleted]]

R-help_at_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. Received on Sun 15 Jun 2008 - 18:22:17 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 15 Jun 2008 - 19:30:40 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive