[R] Removing rows in a data frame containing string in rownames
William Dunlap
wdunlap at tibco.com
Mon Nov 17 04:22:48 CET 2014
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:
> 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
>
> ______________________________________________
> 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]]
More information about the R-help
mailing list