read.table - In R, how to read file with custom end of line (eol) -


i have text file read in r (and store in data.frame). file organized in several rows , columns. both "sep" , "eol" customized.

problem: custom eol, i.e. "\t&nd" (without quotations), can't set in read.table(...) (or read.csv(...), read.csv2(...),...) nor in fread(...), , can't able find solution.

i'have search here ("[r] read eol" , other don't remember) , don't find solution: 1 preprocess file changing eol (not possible in case because fields can find \n, \r, \n\r, ", ... , reason customization).

thanks!

you approach 2 different ways:

a. if file not wide, can read desired rows using scan , split desired columns strsplit, combine data.frame. example:

# provide reproducible example of file ("raw.txt" here) starting your_text <- "a~b~c!1~2~meh!4~5~wow" write(your_text,"raw.txt"); rm(your_text)    eol_str = "!" # whatever character(s) rows divide on sep_str = "~" # whatever character(s) columns divide on  # read , parse text file    # scan gives array of row strings (one string per row) # sapply strsplit gives list of row arrays (as many elements per row columns) f <- file("raw.txt") row_list <- sapply(scan("raw.txt", what=character(), sep=eol_str),                     strsplit, split=sep_str)  close(f)  df <- data.frame(do.call(rbind,row_list[2:length(row_list)])) row.names(df) <- null names(df) <- row_list[[1]]  df #   b   c # 1 1 2 meh # 2 4 5 wow 

b. if doesn't work, agree @bondeddust need external utility -- can invoke in r system() , find/replace reformat file read.table. invocation specific os. example: https://askubuntu.com/questions/20414/find-and-replace-text-within-a-file-using-commands . since note have \n, , \r\n in text already, recommend first find , replace them temporary placeholders -- perhaps quoted versions of -- , can convert them after have built data.frame.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -