bapors
bapors

Reputation: 909

Making a loop with U-test in R

I am having a file that I want to apply Whitney-U test in R.

file1_whitneyinput
mismatch    match
0.166737436882143   8.09322497846657
8.65473982362       0.262366658627097
0.0499563   
1.258946118432  
0.466852    
0.224554    
2.59762 
0.654455    

And, I have other files in the similar format,but some files do not have any match or mismatch column (having only one of them):

file2_whitneyinput
mismatch    match
2.736882143         4.09322497846657
0.651739833072362   0.26236673764384
0.0494545453    
0.1445666119
3.463852    
0.2100
0.56762 

file3_whitneyinput
mismatch    
2.336882143        
0.35173
3.043   
0.145666119

file4_whitneyinput
match   
0.913       
0.3517      
2.033   
0.8872
0.112

and there are file5_whitneyinput and so on.

What I am trying to do is analyzing all of them and writing the name of the file and the output of the U-test in the same file as following:

file1_whitneyinput
Wilcoxon rank sum test

data:  ldf$match and ldf$mismatch
W = 11, p-value = 0.7273
alternative hypothesis: true location shift is not equal to 0
###########################
file2_whitneyinput
Wilcoxon rank sum test

data:  ldf$match and ldf$mismatch
W = 10, p-value = 0.5273
alternative hypothesis: true location shift is not equal to 0

What I do now only uses the first file and then stops, and I couldnt print the name of the file either:

library(data.table)
filenames <- list.files("./TRIAL", pattern="*whitneyinput", full.names=TRUE)
for(file in filenames){
     library(tools)
     bases <- file_path_sans_ext(file)
     ldf <- fread(file)
     output <-  wilcox.test(ldf$match , ldf$mismatch, paired=FALSE)
     chars <- capture.output(print(output))
     writeLines(chars, con = file("output.txt"))

}
dev.off()

It gives the output of:

Wilcoxon rank sum test

    data:  ldf$match and ldf$mismatch
    W = 11, p-value = 0.7273
    alternative hypothesis: true location shift is not equal to 0

I have tried to manage it with:

writeLines(chars, con = basename(file("output.txt")))

However, it didn't give any input.

How can I print the name of the files and its analysis results in order in the same file?

Plus: The results can be written in different files too. I have tried it with putting the dev.off inside of the loop, but did not work either.

Upvotes: 1

Views: 233

Answers (1)

minem
minem

Reputation: 3650

try something like this:

rezults <- lapply(filenames, function(x) {
  print(which(filenames == x))
  ldf <- fread(x)
  if (all(c("match", "mismatch") %in% colnames(ldf))) {
    output <-  wilcox.test(ldf$match , ldf$mismatch, paired=FALSE)
  } else {
    output <- "No data"
  }
  chars <- capture.output(print(output))
  chars
  }
)


rez <- lapply(seq_along(rezults), function(x) {
  c("filename is:", filenames[x], rezults[[x]])
  }
)
rez

writeLines(unlist(rez), con = file("output.txt"))

Upvotes: 2

Related Questions