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

北战南征 提交于 2020-01-14 03:08:08

问题


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.


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/38297989/how-to-pair-rows-in-a-data-frame-with-many-columns-using-dplyr-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!