0

I have around 850 txt files each of around 15 rows each. I want to separate each row of the txt files to be on their own by a loop but I cant seem to do it; for example, I would like to have 850x15=12,750 rows. How can I build a loop to do so? I have tried the readLines code, but it has not worked so far.

path<-"/Users/joseantonio/folder/"
list<-list.files(path, pattern="*.txt")

for (i in list) {
    text<-readLines[i]
    i=i+1
}

I am a beginner at R. Any help will be very much appreciated.

  • use `count.fields` to get the number of columns of a text file. Then use `scan` to read the file as a vector. Now with the counts from `count.fields`, extract the columns from the vector – Sathish Mar 06 '17 at 09:42

1 Answers1

-1

Create a list for your results to start

results <- list()

Then you can use the for loop, but you should mind the correct syntax for function calls (using brackets).

for (i in list) {
    text <- readLines(i)
    results <- append(results, text)
}

results will then contain an array for each of the documents, containing all lines.

PinkFluffyUnicorn
  • 1,260
  • 11
  • 20