I'm having a file with many lines, which I want to transform in a data frame to do some data science.
Reading line by line I founding a little code snippet, which not seems to be working well. But that is not the main problem. First of all, i want only save the lines which are having the string "CANFD" inside. And I know that is not working with a standard if construct, because of the vector. The substring is working and yes the numbers are correct.
fileName <- "Data2F001new.ASC"
conn <- file(fileName,open="r")
linn <-readLines(conn)
for (i in 1:length(linn)){
{
tmp <- substring(linn, 12, 16)
if(tmp=="CANFD"){
system <- substring(linn, 12, 16)
timestamp <- substring(linn, 0, 10)
bytes <- substring(linn, 54, 56)
channel <- substring(linn, 19,20)
}
}
close(conn)
R says me following: The condition has length > 1 and only the first element will be used. The expected Output are the lines with CANFD.
Here is an example how you could do it:
## Create a temp file as dummy example
tmp <- tempfile(fileext = ".txt")
con <- file(tmp, open = "rw")
## Write some lines into the file
writeLines(sample(LETTERS, 1000, replace = TRUE), con)
## read the lines
all_lines <- readLines(con) ## each element represents one line
## filter lines which contain an 'A'
## in your case you want something like grep("CANFD", all_lines)
## see ?grep for further info
all_lines[grep("A", all_lines)]
close(con)