[R] Reducing a matrix
jim holtman
jholtman at gmail.com
Sun Feb 28 05:55:42 CET 2010
Will this work for you:
> x <- 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)
>
> t(sapply(split(x, x$x), function(.grp){
+ sapply(.grp, function(.col) .col[which.max(!is.na(.col))])
+ }))
x y1 y2 y3 y4
3 3 7 16 12 18
6 6 8 NA NA NA
10 10 NA 11 2 NA
14 14 NA 9 NA 8
15 15 NA NA NA 11
50 50 20 NA 13 NA
>
On Sat, Feb 27, 2010 at 7:56 PM, Juliet Ndukum <jpntsang at yahoo.com> 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list