Re: [R] Reducing a matrix

2010-02-27 Thread Dimitris Rizopoulos
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

Re: [R] Reducing a matrix

2010-02-27 Thread jim holtman
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

Re: [R] Reducing a matrix

2010-02-27 Thread David Winsemius
On Feb 27, 2010, at 8:43 PM, Jorge Ivan Velez wrote: Hi Juliet, Here is a suggestion using aggregate(): # aux function foo <- function(x){ y <- sum(x, na.rm = TRUE) ifelse(y==0, NA, y) } # result aggregate(df[,-1], list(df$x), foo) That does work in this ex

Re: [R] Reducing a matrix

2010-02-27 Thread Jorge Ivan Velez
Hi Juliet, Here is a suggestion using aggregate(): # aux function foo <- function(x){ y <- sum(x, na.rm = TRUE) ifelse(y==0, NA, y) } # result aggregate(df[,-1], list(df$x), foo) Here, df is your data. HTH, Jorge On Sat, Feb 27, 2010 at 7:56 PM, Juliet Ndu

Re: [R] Reducing a matrix

2010-02-27 Thread Linlin Yan
Try this: df[!duplicated(df[,'x']),] On Sun, Feb 28, 2010 at 8: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

[R] Reducing a matrix

2010-02-27 Thread Juliet Ndukum
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.