[R] Reducing a matrix
Linlin Yan
yanlinlin82 at gmail.com
Sun Feb 28 02:12:35 CET 2010
Try this:
df[!duplicated(df[,'x']),]
On Sun, Feb 28, 2010 at 8:56 AM, 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.
>
More information about the R-help
mailing list