0

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.

JEHA
  • 3
  • 2

1 Answers1

1

Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.


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)

thothal
  • 16,690
  • 3
  • 36
  • 71
  • Thanks, now i have all the linenumber with "CANFD". But how can i read them now? – JEHA Aug 09 '19 at 07:57
  • Not clear what you are asking, the code above returns all lines (i.e. a vector where each element corresponds to one line in the doc) which have an `A` in it ( `CANFD` in your case). Without your raw data and further info, i cannot help further. – thothal Aug 09 '19 at 08:43
  • Yep, it returns only the line numbers in a vector. I need the whole content of the line. – JEHA Aug 09 '19 at 08:47