Re: [R] equivalent of excel "sumif"

From: Henrique Dallazuanna <wwwhsd_at_gmail.com>
Date: Tue, 04 Mar 2008 11:53:15 -0300

You can try this:

newData <- lapply(list(df1, df2), function(x)split(x[-1], x$lgdcm)) do.call('rbind', lapply(names(newData[[1]]),

                     function(x)newData[[1]][[x]]+newData[[2]][[x]]))

On 04/03/2008, Luis Ridao Cruz <Luisr_at_frs.fo> wrote:
> R-help,
>
> I have two data frames df1 and df2:
>
> >head(df1)
> lgdcm 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
> 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
> 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
> 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
> 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
> 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
> ...
> ...
>
> > head(df2)
> lgdcm 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
> 34 NA NA 1 NA NA NA NA NA NA NA NA NA NA NA NA NA
> 36 NA NA 1 NA NA NA NA NA NA NA NA NA NA NA NA NA
> 40 NA NA 1 1 NA NA NA NA NA NA NA NA NA NA NA NA
> 41 NA NA NA NA 3 NA NA NA NA NA NA NA NA NA NA NA
> 42 NA NA NA NA 7 NA NA NA NA NA NA NA NA NA NA NA
> 43 NA NA NA 1 6 NA NA NA NA NA NA NA NA NA NA NA
> ....
> ....
>
> I want to fill up the columns of df1 (0,1,2,,,,15)
> with columns from df2 (0,1,2,,,,15) with column 'lgdcm' as the
> criterion for matching.
>
> There is a function in excel which does this type of thing, something
> like:
> SUMIF(range,criteria,sum_range)
>
> Can anyone help with this?
>
> Thanks in advance.
>
> ______________________________________________
> 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.
>

-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

______________________________________________
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.
Received on Tue 04 Mar 2008 - 14:57:02 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 04 Mar 2008 - 15:30:19 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive