2

I have a very stupid question. It has been already asked, but none of the solutions provided seem to work with me. I am looping over a list containing different data frames, to perform an analysis and save an output file named differently for each input data frame. The name would be something like originalname_output.txt. I wrote this piece of code which seems to work fine (does all the analysis in the correct ways), but gives an error when coming to the write.table part.


library(qqman)
library(QuASAR)

list_QuASAR <- list (Fw, Rv, tot) #all of the are dfs

for (i in list_QuASAR){
  output <- fitQuasarMpra(i[,2], i[,3], i[,4])
  print(sum(output$padj_quasar<0.1))
  qq(output$pval3, col = "black", cex = 1)
  write.table(output, paste0("quasar_output/", i, "_output.txt"), col.names = T, sep = "\t")
}

fitQuasarMpra is a function of a package called QuASAR. Of course the subdirectory called quasar_output already exists.

The error I am getting is:

Error in file(file, ifelse(append, "a", "w")) : 
  invalid 'description' argument
In addition: Warning message:
In if (file == "") file <- stdout() else if (is.character(file)) { :
  the condition has length > 1 and only the first element will be used

I know it's a trivial problem but I am currently stuck. I may consider to switch and use lapply, but then I may encounter the same problem and I wanted to solve this first. Many thanks for you help.

1 Answer 1

4

You're trying to use a data frame object (i) as part of a file name; i.e. the data frame itself, not its name. You could try iterating over a named list instead:

list_QuASAR <- list (Fw = Fw,Rv = Rv,tot = tot)

for (i in names(list_QuASAR)){
  output <- fitQuasarMpra(list_QuASAR[[i]][,2], list_QuASAR[[i]][,3], list_QuASAR[[i]][,4])
  print(sum(output$padj_quasar<0.1))
  qq(output$pval3, col = "black", cex = 1)
  write.table(output, paste0("quasar_output/", i, "_output.txt"), col.names = T, sep = "\t")
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! That worked as expected. Unfortunately I am still a beginner and I miss some basic knowledges like this one...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.