2

screen shot of data

Hi,

My task is to read selected columns from over 100 same formatted .csv files in a folder, and to cbind into a big large file using R. I have attached a screen shot in this question for a sample data file.

This is the code I'm using:

    filenames <- list.files(path="G:\\2014-02-04")
    mydata <- do.call("cbind",lapply(filenames,read.csv,skip=12))

My problem is, for each .csv file I have, the first column is the same. So using my code will create a big file with duplicate first columns... How can I create a big with just a single column A (no duplictes). And I would like to name the second column read from each .csv file using the value of cell B7, which is the specific timestamp of each .csv file.

Can someone help me on this?

Thanks.

Vicki1227
  • 489
  • 4
  • 6
  • 19
  • 1
    For your first question, you can skip the first column of the file by specifying `colClasses=c("NULL","numeric")` in your `read.csv` call. – nrussell Oct 01 '14 at 14:57
  • The value of B7 is a bad column name and would give you trouble later on. Also, it appears to be data and not a variable name. You should store all B7 values in a separate vector. – Roland Oct 01 '14 at 15:07
  • @Roland Hi, can you specify how I can store all B7 values from each file into a vector? Thanks. – Vicki1227 Oct 01 '14 at 15:13
  • Use the `skip` and `nrows` arguments to read only the 7th line, subset, and combine (using `c`). – Roland Oct 01 '14 at 15:17
  • @nrussell Thanks for your reply. Your code works. But how can I keep only one column A in my big file without reading the remainings? – Vicki1227 Oct 01 '14 at 15:19
  • I think the quickest way would be to just read in both columns of the first file (`filenames[1]`), use `filenames[-1]` in your `lapply` procedure, and then just combine the object from the first file with the object created from all of the other files. – nrussell Oct 01 '14 at 15:25

0 Answers0