I have 507 tables with (pattern = "human").
Inside each table there are a number of columns that I want to compare via Student's t-test. Before I learned 'functions', I was initializing 16 columns in my data.frame and copying a lot of code for 16 comparisons. :( But I want to simplfy the code by using functions.Q: Do I need a counter of some sort or to use 'cbind'? or what??
Any suggestions?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)
My expected output should be:
source.file.name, tag[i], tag[j], ...
files_to_test[1], #, #, ...
files_to_test[2], #, #, ...
What I am trying to do is append or fold my newly tabulated ttest data with the earlier ttest data, either by cbind or data.frame. I'm not sure.
I think you need to move the tagging operation inside the loop:
# 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.
}
Have no data to play with... Something like this?
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)
}
精彩评论