I am trying to read multiple files in R from the working directory and would like to read like to read from the 7th row for each file. I am not sure how can i do that
I found how to read an individual file with this:
data = read.csv(file.choose (), skip = 6 )
or I can read multiple files like this:
j = list.files()
d = lapply(j, read.csv)
could you please help me with how can i read multiple files starting from the 7th row?
In addition to @James’s answer, using
lapplyonly reads the files into a list, not into a commondata.frame. From your question it is not obvious if you want this. But I’ll add it for completeness sake anyway.To be able to identify to which file a row in the common
data.framebelonged originally, I often add a column with the filename. In pseudo-code this would look something like:Alternatively, you could use the awesome
plyrlibrary, which does the exact same thing in:I have not tested this pseudo-code, so it could be that there are some flaws yet. But you get the basic idea. One problem for example could be that
ldplydoes not automatically adds the filename as a column. Then you need to use the function call as I did usinglapply. In that case,ldplysaves you thedo.callstep. Note thatplyrsupports a progress bar (nice for long processes) and parallel processing.note:
jandd. This makes the code easier to read.