How to extract the first line from a text file?
To read the first line of a file, you can do:
con <- file("file.txt","r")
first_line <- readLines(con,n=1)
close(con)
To write it out, there are many options. Here is one:
cat(first_line,file="first_line.txt")
Another way to do it is to read it with read.table() like this:
read.table(file = 'file.txt',header = F,nrows = 1)
This is a simple way to do it, and you can get your data separated into columns which makes it easier to work with.