将文本数据导入R并删除无关的标题和其他不需要的文本

时间:2012-11-14 23:28:51

标签: r text import text-files

我有一个大型文本文件,其中包含统一犯罪报告中的数据。理想情况下,我想要做的只是导入数据并省略文件中的其他无关的东西。实际数据由空格分隔,当数据进入另一个“页面”时,标题信息自身重复。我首先尝试使用以下代码导入数据(并且只导入数据)并手动添加我自己的标题:

  data <- read.fwf("2010SHRall.txt", 
        c(-4,3,8,2,4,5,6,5,4,3,3,4,4,3,3,4,6,5,3,6,26,3),   
        skip=5,       
        col.names=c("AGE","AGENCY","G","MO","HOM","INC","SIT","VA","VS","VR","VE","OA","OS","OR","OE","WEAP","REL","CIR","SUB","AGENCYNAME","STATE"), 
        strip.white=FALSE)

这是有效的,然后在第51行它退出。我绝对是一个新手R程序员,我尝试谷歌答案以及搜索Stack Overflow,但我不知道从哪里开始。以下是我要导入的文本文件的a link。同样,我正在尝试导入数据并删除任何具有标题信息或完整数据集不需要的其他部分的行。

任何人都可以提供的帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

这应该可行:

text <- readLines('/tmp/2010SHRall.txt')
group.start <- '^      AGENCY'
group.end <- '(^B)|(^0END OF GROUP)'
data <- character()
inside.group <- FALSE
for (line in text) {
  if (inside.group) {
    if (grepl(group.end, line))
      inside.group <- FALSE
    else
      data <- append(data, line)
  } else if (grepl(group.start, line)) {
    inside.group <- TRUE
  }
}
read.fwf(textConnection(data),
         widths=c(-4,3,8,2,4,5,6,5,4,3,3,4,4,3,3,4,6,5,3,6,26,3),
         header=FALSE,
         col.names=c("AGE","AGENCY","G","MO","HOM","INC","SIT","VA","VS","VR","VE","OA","OS","OR","OE","WEAP","REL","CIR","SUB","AGENCYNAME","STATE"), 
         strip.white=TRUE)

它将所有行保留在与group.startgroup.end正则表达式匹配的行之间,并丢弃其余行。