[R] Row Sum, exclude positive values
Nordlund, Dan (DSHS/RDA)
NordlDJ at dshs.wa.gov
Wed Jul 16 02:27:19 CEST 2008
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Jim Lemon
> Sent: Tuesday, July 15, 2008 5:03 PM
> To: Rheannon
> Cc: r-help at r-project.org
> Subject: Re: [R] Row Sum, exclude positive values
>
> On Tue, 2008-07-15 at 14:35 -0700, Rheannon wrote:
> > Hello,
> >
> > I'd like to sum the values of a row from the first negative
> number (FN) to
> > the last negative number (LN), but not add any positive
> values to the sum.
> > Then apply this to each row of the data frame.
> >
> > For example if I have a dataframe with Row 1 values
> > DF = (4, 3, 2, 1, 0, -1, -2, -3, -2, 2, 1, -1, -2, -3, -2, -1, 1, 2)
> > I would like to sum the numbers from column 6 to column 16,
> but not include
> > column 10 & 11.
> >
> Hi Rheannon,
>
> How about:
>
> sum(DF[DF<0])
>
> Jim
If you actually have a data frame and want to do this row by row, you could do something like
>sumneg <- function(x) sum(x[x<0])
>apply(DF,1,sumneg)
Hope this is helpful,
Dan
Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204
More information about the R-help
mailing list