Function 我的函数g(x,y)不在data.frame中保存P值

Function 我的函数g(x,y)不在data.frame中保存P值,function,r,dataframe,Function,R,Dataframe,我有507张带有(pattern=“human”)的桌子。 在每个表中有许多列,我想通过Student的t检验进行比较。在学习“函数”之前,我在data.frame中初始化了16列,并复制了大量用于16次比较的代码(但我想通过使用函数简化代码 Q:我需要某种计数器还是使用“cbind”?或者什么?? 有什么建议吗 files_to_test <- list.files(pattern="human") num_files <- length(files_to_test) #

我有507张带有(pattern=“human”)的桌子。
在每个表中有许多列,我想通过Student的t检验进行比较。在学习“函数”之前,我在data.frame中初始化了16列,并复制了大量用于16次比较的代码(但我想通过使用函数简化代码

Q:我需要某种计数器还是使用“cbind”?或者什么??
有什么建议吗

files_to_test <- list.files(pattern="human")  
num_files <- length(files_to_test)  

## Function: Calculate t-test P-values ##
g<-function(compareA,compareB) {
   for (i in 1:num_files){
      temp <- read.table(files_to_test[i], header=TRUE, sep="\t")
      ## Obtain Columns To Compare ##
      colA <- temp[compareA]
      colB <- temp[compareB]
      ttr <- t.test(colA, colB, var.equal=TRUE)
      tt_pvalues[i,1] <- ttr$p.value
      }
tag <- paste(compareA, compareB, sep="_Vs_")
tt_titles <- data.frame(tag,tt_titles) # Here is my problem.  
ALL_pvalues <- data.frame(tt_pvalues, ALL_pvalues) # Here is my problem.  
}

## Comparison 1
compareA <-"log_b" 
compareB <-"log_b_rich"
g(compareA,compareB)

## Comparison 2
compareA <-"fc_Etoh_CDT_tot_poly"
compareB <-"log_b_rich"
g(compareA,compareB)

我想做的是通过cbind或data.frame将新制表的ttest数据与早期的ttest数据进行附加或折叠。我不确定。

没有数据可以处理……类似的情况

g <- function(compareA, compareB) {
   tt_pvalues <- NULL
   for (i in 1:num_files){
      temp <- read.table(files_to_test[i], header=TRUE, sep="\t")
      colA <- temp[compareA]
      colB <- temp[compareB]
      ttr <- t.test(colA, colB, var.equal=TRUE)
      tt_pvalues[i] <- ttr$p.value 
      }
   out <- data.frame(files = files_to_test, pval = tt_pvalues)
   return(out)
}

g我认为您需要在循环中移动标记操作:

# Pre-allocate tag[i] outside the loop
tag <- vector("character", length=num.files)

g<-function(compareA,compareB) {
   for (i in 1:num_files){
      temp <- read.table(files_to_test[i], header=TRUE, sep="\t")
      ## Obtain Columns To Compare ##
      colA <- temp[compareA]
      colB <- temp[compareB]
      ttr <- t.test(colA, colB, var.equal=TRUE)
      tt_pvalues[i,1] <- ttr$p.value
   tag[i] <- paste(compareA, i, "Vs" compareB, i sep="_")
      }

tt_titles <- data.frame(tag, tt_titles) # Here is my problem.  
ALL_pvalues <- data.frame(tt_pvalues, ALL_pvalues) # Here is my problem.  
}
#在循环外部预先分配标记[i]
标签
# Pre-allocate tag[i] outside the loop
tag <- vector("character", length=num.files)

g<-function(compareA,compareB) {
   for (i in 1:num_files){
      temp <- read.table(files_to_test[i], header=TRUE, sep="\t")
      ## Obtain Columns To Compare ##
      colA <- temp[compareA]
      colB <- temp[compareB]
      ttr <- t.test(colA, colB, var.equal=TRUE)
      tt_pvalues[i,1] <- ttr$p.value
   tag[i] <- paste(compareA, i, "Vs" compareB, i sep="_")
      }

tt_titles <- data.frame(tag, tt_titles) # Here is my problem.  
ALL_pvalues <- data.frame(tt_pvalues, ALL_pvalues) # Here is my problem.  
}