【问题标题】:How to divide combinations of rows using dplyr or another method in R?如何使用 dplyr 或 R 中的其他方法划分行组合?
【发布时间】:2021-10-06 15:47:46
【问题描述】:
site <- rep(1:4, each = 8, len = 32)
rep <- rep(1:8, times = 4, len = 32)
treatment <- rep(c("A.low","A.low","A.high","A.high","A.mix","A.mix","B.mix","B.mix"), 4)
sp.1 <- sample(0:3,size=32,replace=TRUE)
sp.2 <- sample(0:2,size=32,replace=TRUE)
df.dummy <- data.frame(site, rep, treatment, sp.1, sp.2)

最终的数据框如下所示

对于每个站点,我想总结不同的组。两个例如:“A.low / A.high”=“sp.1/sp.1”; “A.low/A.mix”=“sp.1/sp.2”。正如您将注意到的,每个站点都有两个,我希望在我的最后几列中所有这些排列。我的最终产品类似于:

site  rep   treatment      value
  1.  1/3.  A.low/A.high.   Inf
  1.  1/4.  A.low/A.high.   1

我开始使用 dplyr,但我真的不知道如何继续使用所有组合

  df.dummy %>% 
  group_by(site) %>% 
  summarise(value.1 = sp.1[treatment = "A.low"] / sp.1[treatment = "A.high"])

【问题讨论】:

    标签: r dplyr combinations permutation


    【解决方案1】:

    您可以使用 reshape2 以更易于使用的格式获取数据。

    下面的代码将 sp.1 和 sp.2 数据分开。使用acast,以便每个数据框由每个站点的一行组成,每一列是一个唯一的样本,其值来自 sp.1 和 sp.2。

    将列命名为独特的名称,并将数据框与 cbind 结合起来。

    现在可以根据您的要求比较每一列。

    library(dplyr)
    library(reshape2)
    
    ##your setup
    site <- rep(1:4, each = 8, len = 32)
    rep <- rep(1:8, times = 4, len = 32)
    treatment <- rep(c("A.low","A.low","A.high","A.high","A.mix","A.mix","B.mix","B.mix"), 4)
    sp.1 <- sample(0:3,size=32,replace=TRUE)
    sp.2 <- sample(0:2,size=32,replace=TRUE)
    df.dummy <- data.frame(site, rep, treatment, sp.1, sp.2)
    
    ##create unique ids and create a dataframe containing 1 value column
    sp1 <- df.dummy %>% mutate(id = paste(rep, treatment, sep = "_")) %>% select(id, site, rep, treatment, sp.1)
    sp2 <- df.dummy %>% mutate(id = paste(rep, treatment, sep = "_")) %>% select(id, site, rep, treatment, sp.2)
    
    ##reshape the data so that each treament and replicate is assigned a single column
    ##each row will be a single site
    ##each column will contain the values from sp.1 or sp.2
    sp1 <- reshape2::acast(data = sp1, formula = site ~ id)
    sp2 <- reshape2::acast(data = sp2, formula = site ~ id)
    
    ##rename columns something sensible and unique
    colnames(sp1) <- c("low.1.sp1", "low.2.sp1", "high.3.sp1", "high.4.sp1",
                       "mix.5.sp1", "mix.6.sp1", "mix.7.sp1", "mix.8.sp1")
    colnames(sp2) <- c("low.1.sp2", "low.2.sp2", "high.3.sp2", "high.4.sp2",
                       "mix.5.sp2", "mix.6.sp2", "mix.7.sp2", "mix.8.sp2")
    
    ##combine datasets
    dat <- sp1 %>% cbind(sp2)
    
    ##choose which columns to compare. Some examples shown below
    dat <-  dat %>% mutate(low.1.sp1/high.3.sp1, low.1.sp1/high.4.sp1,
                           low.2.sp1/high.3.sp2)
    
    

    【讨论】:

      猜你喜欢
      • 2020-05-10
      • 1970-01-01
      • 2016-09-23
      • 2020-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-26
      相关资源
      最近更新 更多