Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reading a specific line of a text file in R

Tags:

file

r

line

I have a file in txt format separated by tabs. The question that I have is that if its possible to jump to an specific line, but without using a for loop, for example if I want to jump to the second line I have done this:

  fileName="table.txt"
  con=file(fileName,open="r")
  for (i in 1:2){
      ctable<-readLines(con,n=1)
  }

but I dont want to use a for loop, how I can do that? Thanks

like image 960
Layla Avatar asked Oct 24 '12 01:10

Layla


People also ask

How do I read a line from a text file in R?

Read Lines from a File in R Programming – readLines() Function. readLines() function in R Language reads text lines from an input file. The readLines() function is perfect for text files since it reads the text line by line and creates character objects for each of the lines.

What does readLines return in R?

readLines returns a character object for each row of the data frame, whereby columns are separated by commas.

How do I go from line to line in R?

Open the script file inside your RGui and press Ctrl+R to run line by line (you need to press many times though;)). However I would recommend to use RStudio for the convenient work with R. In this case you run line by Ctrl+Enter. Or you may modify your script to print() (or cat() ) the objects.


1 Answers

Use read.table() and provide the number of lines to skip in the skip argument. Type in ?read.table at the console for more information on additional arguments and wrappers that you can use.

like image 118
Maiasaura Avatar answered Oct 06 '22 01:10

Maiasaura