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
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
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
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
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
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.
6 matches
Mail list logo