[R] Retain parts of a matrix

David Winsemius dwinsemius at comcast.net
Mon Nov 28 05:35:03 CET 2011


On Nov 27, 2011, at 10:15 PM, Katrina Bennett wrote:

> Hi all,
>
> I'm working to apply a function that will generate a matrix of  
> results only
> when a specific criteria is met.
>
> I want my final results to be a matrix with both the values that  
> meet the
> criteria (the results of the function), and those that to do in the  
> same
> positions in the matrix (the original numbers).
>
> Here's a sample of what I would like to do:
>
> t.mean.1.c <- c(-15, -20, -30, -20, -25, -35, -40, -8, 9, 10)
> cdem <- c(300, 400, 700, 900, 1000, 250, 200, 300, 500, 650, 650,  
> 1200,
> 1400, 3200, 2000)
> cdem.mat <- matrix(NA, 10, 15)
> cdem.mat <- cdem.mat(sapply(cdem[which(cdem >= 900)], function(x)
......................^
cdem.mat is not a function, cannot use "(".


> get("t.mean.1.c") - (x * -0.0065)))
>
> I want cdem.mat to be the same size as the the first call of it,  
> with NA
> values where the cdem is < 899.

You are only offering 10 values as replacements and an unequal number  
of condition regardless of whether you count the qualifying cdem  
conditions (6) or the total conditions(15). So you will need to be  
more clear about how you want the conditions to apply to values. They  
stepping the audience through the first few decisions and say where  
you want the values placed. Maybe you should also say whether you  
expect values to be recycled (the default with matrix assignment).

I suppose it is possible that you want the results of this:

cdem.mat[cdem>899] <-  t.mean.1.c - (cdem * -0.0065)

.... but I'm guessing not.

> I thought this would fill it in but it
> seems to just over write it and I only end up with the rows where  
> cdem is
> greater than 900.
>
> Thank you,
>
> Katrina
>
>
>
> -- 
> Katrina E. Bennett
> PhD Student
> University of Alaska Fairbanks
> International Arctic Research Center
> 930 Koyukuk Drive, PO Box 757340
> Fairbanks, Alaska 99775-7340
> 907-474-1939 office
> 907-385-7657 cell
> kebennett at alaska.edu
>
>
> Personal Address:
> UAF, PO Box 752525
> Fairbanks, Alaska 99775-2525
> bennett.katrina at gmail.com
>
> 	[[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
West Hartford, CT



More information about the R-help mailing list