[R] drop rows

Peter Wilkinson pwilkinson at videotron.ca
Thu Jul 1 06:45:48 CEST 2004


his ...

hmmmm looks like your working with microarray data as well .... The actual 
matrix that I am working with is 19000 x 340

Thanks for the help, that was perfect.  I am still getting used to the R 
language way of doing things.I will look into the apply function as you 
have written it.

Peter

At 12:22 AM 7/1/2004, Erin Hodgess wrote:
>Hi Peter!
>
>Here is an example:
>
> > x
>             [,1]        [,2]        [,3]       [,4]        [,5]
>  [1,]  2.1632497  0.43219960  0.05329827  0.1484550  2.12996660
>  [2,]  0.0000000  0.00000000  0.00000000  0.0000000  0.00000000
>  [3,] -1.2230673  0.83467155 -0.14820752 -0.1012919 -0.04410457
>  [4,] -0.5397403  0.92664487 -0.30390539  0.3105849 -0.69958321
>  [5,]  1.0112805  1.13063148 -1.59802451  0.7597861 -0.72821421
>  [6,] -1.1170756 -0.05128944  0.02755781 -0.8896866  0.12294861
>  [7,]  0.0000000  0.00000000  0.00000000  0.0000000  0.00000000
>  [8,]  0.7043937  0.82557039 -1.38759266  0.5266536  0.67345991
>  [9,]  0.7522765  0.25513348 -1.00076227  0.1141770  1.70003769
>[10,]  0.3371948 -1.48590028 -0.67115529 -0.8242699  1.32741665
> > #This takes out the rows with ANY zeros
> > x[!apply(x,1,function(x)any(x)==0),]
>            [,1]        [,2]        [,3]       [,4]        [,5]
>[1,]  2.1632497  0.43219960  0.05329827  0.1484550  2.12996660
>[2,] -1.2230673  0.83467155 -0.14820752 -0.1012919 -0.04410457
>[3,] -0.5397403  0.92664487 -0.30390539  0.3105849 -0.69958321
>[4,]  1.0112805  1.13063148 -1.59802451  0.7597861 -0.72821421
>[5,] -1.1170756 -0.05128944  0.02755781 -0.8896866  0.12294861
>[6,]  0.7043937  0.82557039 -1.38759266  0.5266536  0.67345991
>[7,]  0.7522765  0.25513348 -1.00076227  0.1141770  1.70003769
>[8,]  0.3371948 -1.48590028 -0.67115529 -0.8242699  1.32741665
> > #This takes out the rows with ALL zeros
> > x[!apply(x,1,function(x)all(x)==0),]
>            [,1]        [,2]        [,3]       [,4]        [,5]
>[1,]  2.1632497  0.43219960  0.05329827  0.1484550  2.12996660
>[2,] -1.2230673  0.83467155 -0.14820752 -0.1012919 -0.04410457
>[3,] -0.5397403  0.92664487 -0.30390539  0.3105849 -0.69958321
>[4,]  1.0112805  1.13063148 -1.59802451  0.7597861 -0.72821421
>[5,] -1.1170756 -0.05128944  0.02755781 -0.8896866  0.12294861
>[6,]  0.7043937  0.82557039 -1.38759266  0.5266536  0.67345991
>[7,]  0.7522765  0.25513348 -1.00076227  0.1141770  1.70003769
>[8,]  0.3371948 -1.48590028 -0.67115529 -0.8242699  1.32741665
> >
>
>  Hope this helps!
>  Sincerely,
>Erin Hodgess
>Associate Professor
>Department of Computer and Mathematical Sciences
>University of Houston - Downtown
>mailto: hodgess at gator.uhd.edu
>
>
>
>From: Peter Wilkinson <pwilkinson at videotron.ca>
>Subject: [R] how to drop rows from a data.frame
>
>here is a snippet of data where I would like to drop all rows that have
>zeros across them, and keep the rest of the rows while maintaining the row
>names (1,2,3, ...10). The idea here is that a row of zeros is an indication
>that the row must be dropped. There will never be the case where there is a
>row(of n columns) with less than 5 zeros in this case(n zeros
>
>I am unsure how to manipulate the data frame to drop rows whiles keeping
>row names.
>
>Peter
>
>the data (imagine separated by tabs):
>
>        SEKH0001  SEKH0002 SEKH0003 SEKH0004 SEKH0005
>   [1,] 256.1139  256.1139 256.1139 256.1139 256.1139
>   [2,] 283.0741  695.1000 614.5117 453.0342 500.1436
>   [3,] 257.3578  305.0818 257.3578 257.3578 257.3578
>   [4,]   0.0000    0.0000   0.0000   0.0000   0.0000
>   [5,]   0.0000    0.0000   0.0000   0.0000   0.0000
>   [6,]   0.0000    0.0000   0.0000   0.0000   0.0000
>   [7,]   0.0000    0.0000   0.0000   0.0000   0.0000
>   [8,] 257.0000  257.0000 257.0000 257.0000 257.0000
>   [9,] 305.7857 2450.0417 335.5428 305.7857 584.2485
>[10,]   0.0000    0.0000   0.0000   0.0000   0.0000
>
>what I want it to look like:
>
>        SEKH0001  SEKH0002 SEKH0003 SEKH0004 SEKH0005
>   [1,] 256.1139  256.1139 256.1139 256.1139 256.1139
>   [2,] 283.0741  695.1000 614.5117 453.0342 500.1436
>   [3,] 257.3578  305.0818 257.3578 257.3578 257.3578
>   [8,] 257.0000  257.0000 257.0000 257.0000 257.0000
>   [9,] 305.7857 2450.0417 335.5428 305.7857 584.2485
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list