简体   繁体   中英

R How to apply function to rows of grouped dataframe?

Suppose I have a dataframe generated like this

dataframe <- data.frame(name = (rep(c('A', 'B', 'C', 'D'), 25)), probe = rep(number, each = 4), a = rnorm(100), b = (rnorm(100)+1), c = (rnorm(100)+5))

> head(dataframe)
  name probe           a           b        c
1    A     1  0.03394554  2.97384424 4.173368
2    B     1  1.64304498  2.67977648 5.027671
3    C     1  0.35266588  1.62455820 5.664635
4    D     1 -1.24197302  0.29907974 5.243112
5    A     2 -0.20330593  0.45405930 6.603498
6    B     2 -1.06909795 -0.02575508 4.318659

The samples are in the columns. Variables are in the rows.

I need to calculate the ratio (A+B)/(C+D) for very set of samples using the same probe, such as when probe == 1 or probe == 2 . I can groupby by probe.

But it seems functions can be applied to the columns, how to apply functions to the rows in a groupby object?

Thanks for the help!

I'd reshape.

library(dplyr)
library(tidyr)

df %>%
  gather(variable, value, -name, -probe) %>%
  spread(name, value) %>%
  mutate(ratio = (A+B)/(C+D) )

Or we could use recast from reshape2 . It is a convenient wrapper for melt/dcast . We add the new column 'ratio' after the reshape.

library(reshape2)
transform(recast(df, measure.var=c('a', 'b', 'c'),
     probe+variable~name, value.var='value'), ratio= (A+B)/(C+D))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM