[R] apply function across dataframe columns for non-exclusive groups
Jeff Newmiller
jdnewmil at dcn.davis.CA.us
Wed Oct 21 14:11:03 CEST 2015
The calculation appears to be sum(a)/(sum(a)+sum(b)).
library(dplyr)
library(tidyr)
result <- ( this_df
%>% gather( group, truth, -c(a,b) )
%>% group_by( group, truth )
%>% summarise( calc = sum(a)/(sum(a)+sum(b)) )
%>% as.data.frame
)
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
On October 21, 2015 1:30:46 PM GMT+02:00, Alexander Shenkin <ashenkin at ufl.edu> wrote:
>Hello all,
>
>I've been banging my head over what must be a simple solution. I would
>
>like to apply a function across columns of a dataframe for rows grouped
>
>across different columns. These groups are not exclusive. See below
>for an example. Happy to use dplyr, data.table, or whatever. Any
>guidance appreciated!
>
>Thanks,
>Allie
>
>
>desired algorithm: calculate a/(a+b) for each TRUE and FALSE grouping
>of
>columns grp1 and grp2.
>
>this_df = data.frame(a = c(1,2,3,4,5), b = c(7,8,9,10,11), grp1 =
>c(T,T,F,F,F), grp2 = c(F,T,F,T,F))
>
>desired output (doesn't have to be exactly this format, but something
>along these lines):
>
>grp1 T 0.166
>grp1 F 0.286
>grp2 T 0.25
>grp2 F 0.25
>
>______________________________________________
>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