1

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.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 1
    I say this not to be strict, but to make sure I know what you mean: please re-create this question with a reproducible example. Right now, the behavior of your loop will depend on what is in `dane`, and what `list.files` returns. Explanation of reproducible examples are here: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Drew Steen Sep 24 '13 at 18:35
  • So, you understand that you can put a for loop inside a for loop, right? – nograpes Sep 24 '13 at 19:05
  • Have you changed the `file="12bielawica9.txt"` argument in `cat()`? Also, instead of `for (i in myfiles[j])`, you need something like `for (i in read.table(myfiles[j], header=T))`. Maybe you should post the actual code used and a template of what you want to achieve. – Ferdinand.kraft Sep 24 '13 at 19:06
  • nograpes - yes that's truth – pani_lebrun Sep 24 '13 at 19:15
  • @Ferdinand.kraft - well, the first loop is working in a good way, but only for this one file. However, I'd like to expand my calculations on more files, that's why I suppose I cannot use – pani_lebrun Sep 24 '13 at 19:21

1 Answers1

1

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.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Thank you so much! First part is fantastic. It's working, and I'm so glad! Second part not excactly - cuz' I'd like to write my loop in such a way that possible would be to create new files (maybe in another catalog) - called the same (or in the same catalog - very similar) - to know which results came from which files. BUT most of all thanks for you answer - I tried to to this for a week. – pani_lebrun Sep 24 '13 at 20:08