简体   繁体   English

在 R 中读取 dat 文件

[英]Reading a dat file in R

I am trying to read a dat file with ";"我正在尝试使用“;”读取 dat 文件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.我想阅读以“B”等某些字符开头的特定行,而另一行不是感兴趣的问题。 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.我曾尝试使用 read_delim、read.table 和 read.csv2。但由于某些行的长度不同。 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.我期待这个文件中的 ar 数据帧,我可以再次将其写入 csv 文件。

You should be able to do that through readLines你应该能够通过 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]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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