I'm experiencing a very hard time with R lately.
I'm not an expert user but I'm trying to use R to read a plain text (.txt
) file and capture each line of it. After that, I want to deal with those lines and make some breaks and changes in the text.
Here is the code I'm using:
fileName <- "C:/MyFolder/TEXT_TO_BE_PROCESSED.txt"
con <- file(fileName,open="r")
line <- readLines(con)
close(con)
It reads the text and the line breaks perfectly. But I don't understand how the created object line
works.
The object line
created with this code has the class: character
and the length [57]
.
If I type line[1]
it shows exactly the text of the first line. But if I type
length(line[1])
it returns me [1]
.
I would like to know how can I transform this string of length == 1
that contains 518 in fact into a string of length == 518
.
Does anyone know what I'm doing wrong?
I don't need to necessarily use the readLines()
function. I've did some research and also found the function scan()
, but I ended with the same situation of a immutable string of 518 characters but length == 1
.
Hope I've been clear enough about my doubt. Sorry for the bad English.
readLines
returns "A character vector of length the number of lines read." (from?readLines
). That's why each line is length 1. Have you triedread.csv
orread.table
for this? – Sapotaceousstrsplit
– Sapotaceousnchar(line[1])
, it'll give you the number of characters in the first element of list (i.e., the first line of your file).length(list)
tells you the number of lines retrieved from the file; by giving itlength(list[1])
, you're asking it the number of elements in a slice of list, a slice that happens to have a single element in it (which may be a string of length 518 or whatever). – Tercentenarynchar(line[1])
returns me the number os characters on the string. But I wannna know how to access those characters individually. Thestrsplit
function does not satisfy my needs. The best way to describe what I wanna do is to say that I want to read every line ofline
(i.e.:line[1]
,line[2]
, ... ,line[n]
) character by character (blank or not) and make some rearrangements. – Osmium?substr
and?regexp
. – Tercentenary