[R] Reducing a matrix
Dimitris Rizopoulos
d.rizopoulos at erasmusmc.nl
Sun Feb 28 08:40:21 CET 2010
if you don't mind having zeros instead of NAs, then yet another solution is:
df <- read.table(textConnection("x y1 y2 y3 y4
1 3 7 NA NA NA
2 3 NA 16 NA NA
3 3 NA NA 12 NA
4 3 NA NA NA 18
5 6 8 NA NA NA
6 10 NA NA 2 NA
7 10 NA 11 NA NA
8 14 NA NA NA 8
9 14 NA 9 NA NA
10 15 NA NA NA 11
11 50 NA NA 13 NA
12 50 20 NA NA NA"), header = TRUE)
closeAllConnections()
out <- rowsum(df[-1], df$x, na.rm = TRUE)
out$x <- as.numeric(row.names(out))
out
I hope it helps.
Best,
Dimitris
On 2/28/2010 1:56 AM, Juliet Ndukum wrote:
> I wish to rearrange the matrix, df, such that all there are not repeated x values. Particularly, for each value of x that is reated, the corresponded y value should fall under the appropriate column. For example, the x value 3 appears 4 times under the different columns of y, i.e. y1,y2,y3,y4. The output should be such that for the lone value of 3 selected for x, the corresponding row entries with be 7 under column y1, 16 under column y2, 12 under column y3 and 18 under column y4. This should work for the other rows of x with repeated values.
> df
> x y1 y2 y3 y4
> 1 3 7 NA NA NA
> 2 3 NA 16 NA NA
> 3 3 NA NA 12 NA
> 4 3 NA NA NA 18
> 5 6 8 NA NA NA
> 6 10 NA NA 2 NA
> 7 10 NA 11 NA NA
> 8 14 NA NA NA 8
> 9 14 NA 9 NA NA
> 10 15 NA NA NA 11
> 11 50 NA NA 13 NA
> 12 50 20 NA NA NA
>
> The output should be:
>
> x y1 y2 y3 y4
> 1 3 7 16 12 18
> 2 6 8 NA NA NA
> 3 10 NA 11 2 NA
> 4 14 NA 9 NA 8
> 5 15 NA NA NA 11
> 6 50 20 NA 13 NA
>
> Can any write for me a code that would produce these results.
> Thank you in advance for your help.
>
> JN
>
>
>
> [[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.
>
--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center
Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
More information about the R-help
mailing list