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
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.
readLines returns a character object for each row of the data frame, whereby columns are separated by commas.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With