Using the summarise function in package plyr is one way; taking df to be
your data frame with variable names V1-V4,

library(plyr)
summarise(subset(df, V3 == 'text3'), sum = sum(V4))
  sum
1 134

Another is to use the data.table package:

library(data.table)
dt <- data.table(df)
dt[V3 == 'text3', sum(V4)]
[1] 134

A third route would be with package sqldf, something like

sqldf(df, "select sum(V4) from df where V3 == 'text3'")

but sqldf is messed up on my system (probably because I didn't install it
properly) so the code is untested.

HTH,
Dennis

On Thu, Mar 17, 2011 at 3:50 AM, e-letter <inp...@gmail.com> wrote:

> On 15/03/2011, Francisco Gochez <fjgoc...@googlemail.com> wrote:
> > Hi,
> >
> > What you are after is:
> >
> > datasubset <- dataset[ dataset[,3] == "text3", ]
>
> Thank you. For the set
>
> text1,23,text2,45
> text1,23,text3,78
> text1,23,text3,56
> text1,23,text2,45
>
> Is it possible to write a function that selects rows containing
> 'text3' and applies the function 'sum' to values '78' and '56'? The
> control statements described in the document 'an introduction to r'
> (Venables and Smith, 2010) suggest that the if statement would return
> 'true', to prevent a sum function being applied to 'true' results.
>
> ______________________________________________
> R-help@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]]

______________________________________________
R-help@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.

Reply via email to