pani_lebrun
pani_lebrun

Reputation: 19

How to do double "for loop" (first for files, second for datas in files) in R

I've just started with R, I'm a self-taught person and I have a problem with my loop. I was looking for answers but nowhere I can't find anything.

I've got a lot of files with different names and also I've got a lot datas in one file. I made a loop for one file and it works good - it's like this (but it has more calculations):

12bielawica9.txt looks like

NA  NA  NA  0.002   0.002   NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA
NA  NA  NA  0.008   0.006   NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA

but it's much more bigger

It contains results of researches, that's why there's a lot of NAs. Sometimes it looks different like reasearches....(there's less NAs) I also wrote in R

dane = dane = read.table("12bielawica9.txt", header=T)

for (i in dane) {
    x = sd(i, na.rm=T)
    y = length (na.omit(i))
    if (y == 0) {
        cat(file="12bielawica9.txt", append=T, (paste("-","\t")))
    } else if (x == "0") {
        cat(file="12bielawica9.txt", append=T, paste(format(mean(i, na.rm=T) - mean(i, na.rm=T), digits=5), "\t"))
    } else {
        cat(file="12bielawica9.txt", append=T, paste(format(t.test(na.omit(i), conf.level=0.90)$conf.int - mean(i, na.rm=T), digits=5), "\t"))
    }
}

First of all I'd like to create a loop for all files, but when I made a loop like:

myfiles <- list.files(pattern=".*txt")
for (j in 1:length(myfiles)) {
    for (i in myfiles[j]) {
        #LOOP FROM ABOVE
    }
}

And now, list.files returns every file (like a 12bielawica.txt), on which I need to make calculations. Is it clear?

R return just one result for every file (like a conjuction). I'd like to achieve as a result a matrixes for every file (like from the my first loop ). I also don't know how to write a new file automaticaly...

I hope You understand what I mean. Don't be strict for me, please.

Upvotes: 1

Views: 170

Answers (1)

Gregor Thomas
Gregor Thomas

Reputation: 146164

You're close. In your question, the thing you do right before the first (inner) loop is read in the file.

dane = read.table("12bielawica9.txt", header=T)

and then your loop refers to that (for (i in dane)). You still need to read stuff when you wrap it in the outer loop:

myfiles <- list.files(pattern=".*txt")

for (j in 1:length(myfiles)) {
    this_file <- read.table(myfiles[j], header = T)
    for (i in this_file) {
        #LOOP FROM ABOVE
    }
}

Should make the loop work.

You also say you want to store the output in a matrix or something. What you should do is initialize an empty matrix to store the results, and then have the inner loop insert the results. Something like

results_mat <- matrix(NA, nrow = 2, ncol = length(myfiles))
## 1st row will be x, second row will be y, 1 column per file

Then change your inner loop to something like

for (i in dane) {
    x = sd(i, na.rm=T)
    y = length (na.omit(i))
    results_mat[, j] <- c(x, y)
}

You can, of course, expand this to more rows for all of your results... I'll leave that to you. You can also do something like colnames(results_mat) <- myfiles so you can tell what results correspond to what files.

Upvotes: 1

Related Questions