简体   繁体   中英

How to pair rows in a data frame with many columns using dplyr in R?

I have a dataframe containing multiple observations from the control and the experimental cohorts with replicates for each subject.

Here is an example of my dataframe:

subject  cohort    replicate val1   val2
  A     control       1       10     0.1
  A     control       2       15     0.3
  A     experim       1       40     0.7
  A     experim       2       45     0.9
  B     control       1        5     0.3     
  B     experim       1       30     0.0
  C     control       1       50     0.5
  C     experim       1       NA     1.0

I'd like to pair each control observation with its corresponding experimental one for each value to calculate the ratio between the pairs. The desired output would look something like this:

subject  replicate   ratio_val1   ratio_val2
  A         1           4             7
  A         2           3             3
  B         1           6             0
  C         1          NA             2 

Ideally, I'd like to see this implemented with dplyr and pipes.

You can use summarize_at function from dplyr to summarize columns val1 and val2 after grouping the data by subject and replicate . Use [cohort == ...] to pick up the values at the experiment and control group correspondingly for division:

library(dplyr)
df %>% group_by(subject, replicate) %>% 
       summarize_at(vars(contains('val')), 
                    funs("ratio" = .[cohort == "experim"]/.[cohort == "control"]))

# Source: local data frame [4 x 4]
# Groups: subject [?]
#
#   subject replicate val1_ratio val2_ratio
#    <fctr>     <int>      <dbl>      <dbl>
# 1       A         1          4          7
# 2       A         2          3          3
# 3       B         1          6          0
# 4       C         1         NA          2

We can use data.table by reshaping the dataset to 'wide' format.

library(data.table)
dcast(setDT(df1), subject+replicate~cohort, value.var = c("val1", "val2"))[,
          paste0("ratio_", names(df1)[4:5]) := Map(`/`, .SD[,  
      grep("experim", names(.SD)), with = FALSE], 
       .SD [, grep("control", names(.SD)), with = FALSE])][, (3:6) := NULL][]
#    subject replicate ratio_val1 ratio_val2
# 1:       A         1          4          7
# 2:       A         2          3          3
# 3:       B         1          6          0 
# 4:       C         1         NA          2

Or after grouping with 'subject', 'replicate', we loop over the 'val' columns and divide the corresponding elements of 'val' for 'experim' with that of 'control'

setDT(df1)[, lapply(.SD[, grep("val", names(.SD)), with = FALSE], 
   function(x) x[cohort =="experim"]/x[cohort =="control"]) ,
               by = .(subject, replicate)]

Or we can use gather/spread from tidyr

library(dplyr)
library(tidyr)
df1 %>%
   gather(Var, Val, val1:val2) %>%
   spread(cohort, Val) %>% 
   group_by(subject, replicate, Var) %>%
   summarise(ratio = experim/control) %>% spread(Var, ratio)
#    subject replicate  val1  val2
#      <chr>     <int> <dbl> <dbl>
# 1       A         1     4     7
# 2       A         2     3     3
# 3       B         1     6     0
# 4       C         1    NA     2

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