-1
ULTRON <- function(directory, pollutant, id = 1:332) {

        files_full <- list.files(directory, full.names = TRUE)       

        dat <- data.frame()

        for (i in id) {

                dat <- rbind(dat, read.csv(files_full[i]))
        }

    mean(dat[, pollutant], na.rm = TRUE)
}

Can some one explain me in detail what the below line does in the above code

dat <- rbind(dat, read.csv(files_full[i]))

I am not able to understand how the data frame passed in the rbind is working and what it returns.

talat
  • 68,970
  • 21
  • 126
  • 157
  • 2
    A more r-like approach would be `dat <- do.call(rbind, lapply(files_full, read.csv))` instead of `dat <- data.frame()` + the whole `for` loop. – talat May 12 '16 at 16:40
  • 1
    Furthermore this is obviously from the Coursera homework set and has been asked and answered multiple times on SO. – IRTFM May 12 '16 at 16:46
  • 1
    Do a search on `rbind pollutant` for further examples. And please do note that the Coursera courses expect you to use the web page help forums for the course. – IRTFM May 12 '16 at 17:00
  • I understand that i have to use the forums and i have asked similar question there too.. i was not able to understand and thought the larger audience would be able to explain in detail in different ways – Vasant Prabhu May 13 '16 at 07:47
  • @docendodiscimus : i could use that but i need an input for the id, how can i use that code? there is no option to take the id in that code. – Vasant Prabhu May 13 '16 at 07:50

1 Answers1

2

The rbind() function takes two (or more) data frames and joins them together by rows. Put another way, it "stacks" two data frames on top of each other. If one data frame has more or less columns than the other, then R will not allow the operation to happen (though see rbind.fill for an way to do this).

dat <- rbind(dat, read.csv(files_full[i]))

After this operation, dat will be what it was before, with a new data frame appended to it at the bottom. This data frame will be whatever was read in from the files_full array.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360