【问题标题】:R problems with a count nrow inside of a for loop在for循环内计数nrow的R问题
【发布时间】:2021-04-20 23:05:21
【问题描述】:

我试图提取循环内的 nrow 但它保持循环的所有交互的最后计数相同,问题对应于行

n1 <- nrow(Fsd[Fsd[, Group] == Gps[[1]],])  
n2 <- nrow(Fsd[Fsd[, Group] == Gps[[2]],]) 

这是函数的一部分:

Tab_Alpha <- suppressMessages(microbiome::alpha(Phyloseq, index = Index))
    colnames(Tab_Alpha)[1] <- Index 
    
    sd <- data.frame(sample_data(Phyloseq))
    sd[, Index] <- paste(Tab_Alpha[, Index])
    sd <- sd[, c(Index, Group)]
    Com_Group <- combn(unique(as.character(sd[, Group])), 2)
    
    Df <- c()
    Sum.Sq <- c()
    Mean.Sq <- c()
    F.value <- c() 
    Pr.F <- c() 
    group1 <- c()
    group2 <- c() 
    
    # The Loop !!!
    for(i in 1:ncol(Com_Group)){
        Gps <- c(Com_Group[1, i], Com_Group[2, i])
        Fsd <- dplyr::filter(sd, sd[, Group] %in% Gps)
        Aresult <- aov(Fsd[,Index] ~ Fsd[,Group])
        
        ar <- data.frame(list(summary(Aresult)[[1]]))
        
        colnames(ar) <- paste(c("Df", "Sum.Sq", "Mean.Sq", "F.value", "Pr.F"))
        ar <- ar[-c(2), ]
        
        
        
        Df <- rbind(Df, ar[, "Df"])
        Sum.Sq <- rbind(Sum.Sq, ar[, "Sum.Sq"])
        Mean.Sq <- rbind(Mean.Sq, ar[, "Mean.Sq"])
        F.value <- rbind(F.value, ar[, "F.value"])
        Pr.F <- rbind(Pr.F, ar[, "Pr.F"])
        group1 <- rbind(group1, Gps[[1]])
        group2 <- rbind(group2, Gps[[2]])
        
        n1 <- nrow(Fsd[Fsd[, Group] == Gps[[1]],])   # The Problem !!!
        n2 <- nrow(Fsd[Fsd[, Group] == Gps[[2]],])   # The Problem !!!
        
    }
    
    # Code 
    p.signif = c(rep('',length(Pr.F)))
    p.signif[Pr.F >  0.05]  <-'ns'
    p.signif[Pr.F <= 0.05]  <-'.'
    p.signif[Pr.F <= 0.01]  <-'*'
    p.signif[Pr.F <= 0.001] <-'**'
    p.signif[Pr.F <= 0]     <-'***'
    #0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1      
    
    AnovaR <- data.frame(Group, Index, n1, n2, Df, Sum.Sq, Mean.Sq, F.value, Pr.F, group1, group2, p.signif) 

在最终的 data.frame AnovaR 中,我添加 n1 和 n2 以在每次比较中根据 Gps[[1]] 和第二组基于 Gps[[2]] 给出对应于一组元素的数量,但是它只打印所有交互的最后一个计数,我的意思是,如果我有 3 个组(Illumina(n=85)、Pyro454(n=154)和 Sanger(n=41))它必须打印每个组中有多少 nrow ,但在下一个数据帧中,n1 仅显示 85,n2 显示 154,对应于 Illumina (85) 和 Pyro454 (154) 的最后交互

   Group   Index n1  n2 Df      Sum.Sq     Mean.Sq     F.value       Pr(>F)   group1   group2 p.signif
1 SeqTech shannon 85 154  1 0.001703502 0.001703502  0.02169554 8.831391e-01   Sanger Illumina       ns
2 SeqTech shannon 85 154  1 3.646070790 3.646070790 28.68863997 2.403115e-07   Sanger  Pyro454       **
3 SeqTech shannon 85 154  1 6.459171438 6.459171438 66.22950059 2.269616e-14 Illumina  Pyro454       **

但它必须是

    Group   Index n1  n2 ....   n1       n2
1 SeqTech shannon 41  85 .... Sanger   Illumina 
2 SeqTech shannon 41 154 .... Sanger   Pyro454
3 SeqTech shannon 85 154 .... Illumina Pyro454

我尝试在 for 循环之外(循环之前)为 n1 和 n2 添加一个空变量

n1 <- c()
n2 <- c()

然后将其添加到 AnovaR data.frame,但结果保持不变。

但是,如果我在循环内使用 print 效果很好!

.......... code before 

        n1 <- nrow(Fsd[Fsd[, Group] == Gps[[1]],])
        n2 <- nrow(Fsd[Fsd[, Group] == Gps[[2]],])
        print(n1)
        print(n2)

code after ...............

结果是:

myfunction(enterotype, Group = "SeqTech", Index = "shannon")
[1] 41      # first interaction 
[1] 85      # first interaction
[1] 41      # second interaction
[1] 154     # second interaction
[1] 85      # third interaction
[1] 154     # third interaction

如何只修复 n1 和 n2 变量!

顺便说一下,我使用的是方差分析统计。

【问题讨论】:

  • 与其他人相比,即group1, group2`等在循环内执行rbind,其中n1n2在循环内创建,并在每次迭代中被替换.所以,你返回的是上一次迭代的结果

标签: r for-loop


【解决方案1】:

基于显示的代码的修复将与用于初始化“group1”、“group2”等的修复相同。即为“n1”、“n2”和rbind或@987654322创建一个空向量@

# // initialize outside
n1 <- c()
n2 <- c()

然后在循环里面做

n1 <- c(n1, nrow(Fsd[Fsd[, Group] == Gps[[1]],]))
n2 <- c(n2, nrow(Fsd[Fsd[, Group] == Gps[[2]],]))

这些是循环中唯一未初始化且未rbinded 的值,因此它在每次迭代时被替换,返回上次迭代的值


这种初始化,即空向量主要是在我们事先不知道长度的情况下完成的。在这里,它将是“Com_Group”的列数。所以,所有的对象都可以被初始化为类似于

 n1 <- numeric(ncol(Com_Group))
 n2 <- numeric(ncol(Com_group))

然后在for 循环内,使用索引分配给该特定元素

n1[i] <- nrow(Fsd[Fsd[, Group] == Gps[[1]],])
n2[i] <- nrow(Fsd[Fsd[, Group] == Gps[[2]],])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-10
    • 2015-01-19
    • 2022-01-17
    • 2015-01-19
    • 2020-02-03
    • 2018-11-02
    • 1970-01-01
    相关资源
    最近更新 更多