简体   繁体   中英

Reading a dat file in R

I am trying to read a dat file with ";"separated. I want to read a specific line that starts with certain characters like "B" and the other line are not the matter of interest. Can anyone guide me.

I have tried using the read_delim, read.table and read.csv2.But since some lines are not of equal length. So, I am getting errors.

file <- read.table(file = '~/file.DAT',header = FALSE, quote = "\"'",dec = ".",numerals = c("no.loss"),sep = ';',text)

I am expecting ar dataframe out of this file which I can write it to a csv file again.

You should be able to do that through readLines

allLines <- readLines(con = file('~/file.DAT'), 'r')
grepB <- function(x) grepl('^B',x)
BLines <- filter(grepB, allLines)
df <- as.data.frame(strsplit(BLines, ";"))

And if your file contains header, then you can specify

names(df) <- strsplit(allLines[1], ";")[[1]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM