[R] Reducing a matrix

David Winsemius dwinsemius at comcast.net
Sun Feb 28 03:41:00 CET 2010


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 example but might give unexpected results if  
there were sums to 0 of paired -7 and 7's or even multiple values of  
any sort. (Throwing an error might be a good thing if multiple values  
in groups were not expected, but such is not reported as an error in  
this code. ) If the OP :wanted just the first non-NA value within her  
groups then:

 > aggregate(df[,-1], list(df$x), function(x) ifelse(
                                              all(is.na(x)),
                                                   NA,
                                                   na.exclude(x)[1]))
   Group.1 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

Munging the example

 > df[2,2] <- 6
 >
 > aggregate(df[,-1], list(df$x), function(x) ifelse(all(is.na(x)),  
NA, na.exclude(x)[1]))
   Group.1 y1 y2 y3 y4
1       3  7 16 12 18   # first value taken.
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
 > foo <- function(x){
+            y <- sum(x, na.rm = TRUE)
+            ifelse(y==0, NA, y)
+            }
 >
 > # result
 > aggregate(df[,-1], list(df$x), foo)
   Group.1 y1 y2 y3 y4
1       3 13 16 12 18    # summed values appear.
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

>
> Here, df is your data.
>
> HTH,
> Jorge
>
>
> On Sat, Feb 27, 2010 at 7:56 PM, 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.
>>
>
> 	[[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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT



More information about the R-help mailing list