[R] Filtering out rows

Bert Gunter bgunter.4567 at gmail.com
Wed Nov 2 21:09:18 CET 2016


If I understand correctly, this is merely a matter of subscripting,
which is a basic and essential R operation. If you aren't familiar
with subscripting, stop and go through one of the many R web tutorials
that covers this before proceeding. See also ?subset for a simpler but
not  quite so flexible way to do this.

For fitting, lmsreg, like many/most of R's modeing functions has a
'data' argument. To "filter out" all rows of df that have df$case ==
"1511", simply specify

data = df[df$case != "1511", ]

in your call. However, note that if the other variables in your call
are *not* all in df, you'll have to subscript those that aren't ,too;
e.g.

use <- df$case != "1511"
npi_lms=lmsreg(npi_mvmt[use] ~ cnavgpi,data=df[use,])

where the rhs variable is in df but the lhs variable is not.

Finally, note that in the case where data contains all the variables
of the call, you can skip the indexing and just use the subset =
argument of lmsreg.

-- Bert
Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, Nov 2, 2016 at 9:48 AM, Bryan Mac <bryanmac.24 at gmail.com> wrote:
> Hi,
>
> I am looking to filter out rows in my data set, run my analysis and
> then removing the filter. Here is my regression. I would like to
> filter out a row. For example, I would like to filter out a row,
> "Case"=1511.
>
> npi=lm(npi_mvmt~cnavgpi,data=df)
> summary(npi)
> resid.vs.fitt.LS%<a-%{plot(npi,1)}
> resid.vs.fitt.LS
>
> npi_lms=lmsreg(npi_mvmt~cnavgpi,data=df)
> fitted.npi_lms<-fitted(npi_lms)
> resid.npi_lms<-residuals(npi_lms)
> resid.vs.fitt.LMS %<a-%{plot(fitted.npi_lms,resid.npi_lms);
> title("npi_mvmt~cnavgpi")}
> resid.vs.fitt.LMS
>
> plot_cnavgpi_npi_mvmt %<a-%{plot(cnavgpi,npi_mvmt);
> abline(lm(npi_mvmt~cnavgpi,data=df),lwd=2,col=2,lty=1);
> abline(lmsreg(npi_mvmt~cnavgpi,data=df),lwd=2, col=3,lty=1);
> legend('topright',c('OLS','LMS'),lty=c(1,1),lwd=c(2.5,2.5), col=c(2,3));
> title (npi_mvmt~cnavgpi) }
> plot_cnavgpi_npi_mvmt
>
> Best,
> Bryan Mac
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



More information about the R-help mailing list