在R中读取dat文件

时间:2019-01-16 00:14:54

标签: r file

我正在尝试使用“;”读取dat文件。分开。我想阅读以“ B”之类的某些字符开头的特定行,而另一行则无关紧要。谁能指导我。

我尝试使用read_delim,read.table和read.csv2。但是由于某些行的长度不相等。所以,我遇到了错误。

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

我希望这个文件中有一个r数据帧,我可以将它再次写入csv文件。

1 个答案:

答案 0 :(得分:1)

您应该能够通过readLines

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

如果文件包含标题,则可以指定

names(df) <- strsplit(allLines[1], ";")[[1]]
相关问题