[R] Trimming Parallel Vectors.

Douglas Bates bates at stat.wisc.edu
Sun Feb 13 00:27:26 CET 2005


Jagarlamudi, Choudary wrote:
> Good Day All !
> I have a 2-D vector of mode numeric and a parallel 1-D vector of mode numeric. 
> Here are my values.
> 2-D vector         1-D vector
> 80 75 85 80       80
> 70 80 90 80       80  
> 60 70 80 70       70
> 85 75 95 85       85
> 70 60 90 60       70
> My 1-D vector is the average across the rows of my 2-D vector.
> I process each column of the 2-D vector in a loop.
> When I trim my 2-D vector column for values less than 65, I have to trim the corrosponding average value.
> I have a copy of the 1-D vector to recreate the original 1-D vector for each loop iteration.
> My original vectors consist of 54,000 values and I am trying to avoid using loops as much as I can to gain speed.
> Any advice will be greatly appreciated.
>  
> Thanks in advance.
>  
> Choudary Jagarlamudi
> Instructor
> Southwestern Oklahoma State University
> STF 254
> 100 campus Drive
> Weatherford OK 73096
> Tel 580-774-7136

I'm not sure if this is what you had in mind but the rowMeans function 
produces the row-wise means of a matrix.  If you replace some of the 
values in the matrix by the missing value code NA then the means for 
those rows also end up as NA.

 > mat
      [,1] [,2] [,3] [,4]
[1,]   80   75   85   80
[2,]   70   80   90   80
[3,]   60   70   80   70
[4,]   85   75   95   85
[5,]   70   60   90   60
 > rowMeans(mat)
[1] 80 80 70 85 70
 > mat[mat < 65] <- NA
 > mat
      [,1] [,2] [,3] [,4]
[1,]   80   75   85   80
[2,]   70   80   90   80
[3,]   NA   70   80   70
[4,]   85   75   95   85
[5,]   70   NA   90   NA
 > rowMeans(mat)
[1] 80 80 NA 85 NA




More information about the R-help mailing list