r - What's the most idiomatic long table eqivalent of subtracting two data.frame columns? -


in wide table, dividing columns easy:

t=read.table(header=true,text=     " gene time  green   red       g1   t0    10      4       g1   t1    15      6       g2   t0    25      3       g2   t1    32      4") t$ratio = t$green/t$red  ##   gene time green red    ratio ## 1   g1   t0    10   4 2.500000 ## 2   g1   t1    15   6 2.500000 ## 3   g2   t0    25   3 8.333333 ## 4   g2   t1    32   4 8.000000 

i want archive same in long table. is, applying function on 2 or more values match in 1 factor , have pairwise different levels of factor while being able reference each value in computation distinctive factor

the solution found far converting table wide format , proceeding above.

library(dplyr) library(tidyr) t = read.table(text=                " gene  ex  dye  time                   g1    4   r    t0                  g2    3   r    t0                  g1    10  g    t0                  g2    25  g    t0                  g1    6   r    t1                  g2    4   r    t1                  g1    15  g    t1                  g2    32  g    t1              ",header=true)  spread(t,dye,ex) %>%      mutate(dye.ratio = g/r) %>%     select(-g,-r)   ##   gene time dye.ratio ## 1   g1   t0  2.500000 ## 2   g1   t1  2.500000 ## 3   g2   t0  8.333333 ## 4   g2   t1  8.000000 

is there more idiomatic way of performing quite basic task perhaps needs less 3 statements?


Comments