This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Applying a task to several files in R

I would to apply a loop in R to process several files, one file per time. The files have exactly the same pattern, just the string "...split1..." is a crescent number to my files. Then a have files like "...split1...", "...split2..." ... "...split777...". I want output files like in the same logic, in the example: "newsplit1.txt", "newsplit2.txt" ... "newsplit777.txt".

all <- read.table("nsamplescluster.split1.adjusted", header=TRUE, sep=";") all <- all[, -grep("GType", colnames(all))] write.table(all, "newsplit1.txt", sep=";")

Cheers!

r loop sapply lapply

This doesn't have any connection to bioinformatics, right?

As Michael suggested, please make this relevant to bioinformatics or we'll just delete it. As is, this is more appropriate for stackoverflow or an R forum.

I'm closing this up, since it's not a bioinformatics question, as noted below. This allow us to keep the site focused on the topics that the community can help with. Please consider asking your question on Stack Overflow, or better yet, taking a little time to work through a basic R tutorial, which will help with both of the questions you've asked today.

1 answer

here's some example code

> files = Sys.glob("*.txt")
> files
[1] "1.txt"     "2.txt"     "3.txt"
> for(i in files){
  #your code here
}

Now I'm closing this up, since it's really a programming question and not a bioinformatics question. Based on your last two questions, you would benefit from working through a short R tutorial.

Log in to answer this question.