Search code examples
rreadfilequotation-marks

r read a line with quotation marks from a txt file


This might be something easy but I am having issues with it. I want to read the first line of a long .txt file, the line is like this (but much longer):

"15075060" "15085030" "15085040"

and i want to save each of this elements as objects in a vector p, so the vector p should be:

> p
[1] "15075060" "15085030" "15085040"

I use the next code:

setwd("location of the file")                     
fileName="name of the file"
con=file(fileName,open="r")  
line=readLines(con)   
txt = line[[1]]
newTxt <- unlist(strsplit(txt, split = " ")) 
nvar = length(newTxt)
for (i in 1:nvar){ 
  p[i]=newTxt[i]
}

and what i obtain is:

> p
[1] "\""         "15075060\"" "\""         "15085030\"" "\""         "15085040\""

there must be a very easy way to do this but i dont know it


Solution

  • read.table("mytxtfile.txt", nrows = 1)