使用rbind确保现有矩阵行不是覆盖的R函数?

问题描述 投票:0回答:1

我正在尝试使用for语句创建一个循环,该语句将统计测试(wilcoxon.test)分析的变量名称添加到数据帧的单行中所有相关测试统计信息的左侧。

我发现代码排除第二个'for'语句包含'i'引用工作正常 - 所以统计是正确的,但是当我添加代码时(包含引用'i'的for语句并列出'我在'rbind'下指定的向量下,所有以前正确的统计数据都会被一次测试的一组统计结果覆盖。

dta <- c()
max_col_A1.1_OR_df_transp <- ncol(A1.1_OR_df_transp)

  for(j in 2:ncol(numeric_A1.1_OR_df_transp)){ 
  for(i in colnames(A1.1_OR_df_transp[2:max_col_A1.1_OR_df_transp])){
  wilcoxon_input <- as.matrix(c(A1.1_OR_df_transp[1:23,j], A1.1_OR_df_transp[24:76,j]))
  wilcoxon_result <- wilcox.test(wilcoxon_input[1:23], wilcoxon_input[24:76], alternative = "two.sided", paired = FALSE, exact = TRUE)
    dta <- rbind(dta,c(i,(unlist( wilcoxon_result))))
  }
}

预期结果应如下(A):

head(dta)

[1,] "AAA_8.44_753.9742mz"  "1214"      "2.2272910427744e-18"  "0"                       
"two.sided" "Wilcoxon rank sum test"                            
"wilcoxon_input[1:23] and wilcoxon_input[24:76]"
[2,] "AAA_9.03_190.0498mz"  "1059"      "2.02343356121504e-08" "0"                       
"two.sided" "Wilcoxon rank sum test with continuity correction" 
"wilcoxon_input[1:23] and wilcoxon_input[24:76]"

实际结果(B):(注意变量名称(最左边是正确的)但是,其他测试结果正在被复制)

head(dta)

[1,] "AAA_8.44_753.9742mz"  "1214"      "2.2272910427744e-18" "0"                       
"two.sided" "Wilcoxon rank sum test" "wilcoxon_input[1:23] and 
wilcoxon_input[24:76]"
[2,] "AAA_9.03_190.0498mz"  "1214"      "2.2272910427744e-18" "0"                       
"two.sided" "Wilcoxon rank sum test" "wilcoxon_input[1:23] and
r loops rbind write.table
1个回答
0
投票

现在我只能使用一个引用 - 即i,所以以下现在可以完美地运行:

dta <- c()

max_col_A1.1_OR_df_transp <- ncol(A1.1_OR_df_transp)

for(i in colnames(A1.1_OR_df_transp[2:max_col_A1.1_OR_df_transp])){
  wilcoxon_input_CTRL <- A1.1_OR_df_transp_CTRL[1:23,i]
  wilcoxon_input_PATIENT_GROUP <- A1.1_OR_df_transp_PATIENT_GROUP[24:76,i]
  wilcoxon_result <- wilcox.test(wilcoxon_input_CTRL, wilcoxon_input_PATIENT_GROUP, alternative = "two.sided", paired = FALSE, exact = TRUE) 
  dta <- data.frame(unlist(wilcoxon_result),stringsAsFactors=FALSE)
  dta1 <- t(dta)
  dta1 <- cbind(i,dta1)
  output_frame <- rbind(output_frame, dta1)
}
© www.soinside.com 2019 - 2024. All rights reserved.