Skip to content

Removing rows in a data frame containing string in rownames

4 messages · William Dunlap, Steven Yen, Jeff Newmiller

#
I like to remove from a data frame rows with labels containing 
certain string, e.g., "sex" and "rating". Below is a list of the data 
frame and my failed attempt to the rows. Any clues? Thanks.

 > out
                  est     se     t     p disc
p.(Intercept) 26.430 13.605 1.943 0.053
p.sex          3.502  3.930 0.891 0.373 *
p.children     3.693  4.521 0.817 0.414 *
p.occu         0.740  1.116 0.663 0.508
p.rating      -7.897  1.331 5.933 0.000
c.(Intercept)  1.861  0.965 1.929 0.054
c.sex          0.221  0.249 0.889 0.374 *
c.children     0.234  0.289 0.810 0.418 *
c.occu         0.052  0.079 0.663 0.508
c.rating      -0.556  0.102 5.451 0.000
u.(Intercept)  1.943  1.017 1.910 0.057
u.sex          0.221  0.248 0.888 0.375 *
u.children     0.229  0.276 0.827 0.409 *
u.occu         0.054  0.082 0.663 0.508
u.rating      -0.581  0.109 5.331 0.000

 > out<-subset(out,!(names(out) %in% c("sex","rating")))
 > out
                  est     se     t     p disc
p.(Intercept) 26.430 13.605 1.943 0.053
p.sex          3.502  3.930 0.891 0.373 *
p.children     3.693  4.521 0.817 0.414 *
p.occu         0.740  1.116 0.663 0.508
p.rating      -7.897  1.331 5.933 0.000
c.(Intercept)  1.861  0.965 1.929 0.054
c.sex          0.221  0.249 0.889 0.374 *
c.children     0.234  0.289 0.810 0.418 *
c.occu         0.052  0.079 0.663 0.508
c.rating      -0.556  0.102 5.451 0.000
u.(Intercept)  1.943  1.017 1.910 0.057
u.sex          0.221  0.248 0.888 0.375 *
u.children     0.229  0.276 0.827 0.409 *
u.occu         0.054  0.082 0.663 0.508
u.rating      -0.581  0.109 5.331 0.000
#
Try grepl() to do pattern matching in strings.  ("%in%" checks for
equality.)  E.g., using your original 'out' do
   out[ !grepl("sex|rating", rownames(out), ]
to get all but the rows whose names contain the character sequences
"sex" or "rating".

Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Sun, Nov 16, 2014 at 6:31 PM, Steven Yen <syen04 at gmail.com> wrote:

            

  
  
#
Thank you Bill and Dennis. grepl worked great. 
However, for reason I am not figuring out, the 
code worked as I included the procedure 
(subroutine) with a source command, viz.,

   source("z:\\R\\mylib\\me.R")

Compiling the routine into a library/package, as 
I always do, then the command got ignored. Hmm...

Steven
At 10:22 PM 11/16/2014, William Dunlap wrote:

  
  
#
Not clear what you did. Is this an example of FAQ 7.16?
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
On November 16, 2014 7:47:44 PM PST, Steven Yen <syen04 at gmail.com> wrote: