0

我是 R 新手,试图从一个文件中生成大量图表,并在不同数据集之间使用标题。我有一个制表符分隔的纯文本文件,格式如下:

Header: Boston city data
Month    Data1    Data2    Data3
1        1.5      9.1342   8.1231
2        12.3     12.31    1.129
3        (etc...)  

Header: Chicago city data
Month    Data1    Data2    Data3
1        1.5      9.1342   8.1231
2        12.3     12.31    1.129
...

我想为每个城市创建月与 Data1、月与 Data2 和月与 Data2 的图表。

我知道在python中,我可以遍历每一行,如果该行以“标题”开头,则做一些不同的事情,然后以某种方式处理这些数字。我想简单地这样做:

for (data block starting with header) in inf:
    data = read.delim()
    barplot(data, main=header, ylab="Data1", xlab="Month")
    # repeat for Data2, Data3

但我不确定如何实际遍历文件,或者我是否应该按城市将我的文件拆分为许多小文件,然后遍历要读取的小文件列表。

4

2 回答 2

4

您可以使用gsub,grep和的组合strsplit

## get city name
nameSet <- function(x) {
    return(gsub(pattern="Header: (.*) city data", replacement="\\1", x=x))
}

## extract monthly numbers
singleSet <- function(x) {
    l <- lapply(x, function(y) {
        ## split single line by spaces
        s <- strsplit(y, "[[:space:]]+")
        ## turn characters into doubles
        return(as.double(s[[1]]))
    })
    ## turn list into a matrix
    m <- do.call(rbind, l)
    return(m) 
}

## read file
con <- file("data.txt", "r")
lines <- readLines(con)
close(con)

## determine header lines and calculate begin/end lines for each dataset
headerLines <- grep(pattern="^Header", x=lines)
beginLines <- headerLines+2
endLines <- c(headerLines[-1]-1, length(lines))

## layout plotting region
par(mfrow=c(length(beginLines), 3))

## loop through all datasets
for (i in seq(along=headerLines)) {
    city <- nameSet(lines[headerLines[i]])
    data <- singleSet(lines[beginLines[i]:endLines[i]])

    for (j in 2:ncol(data)) {
        barplot(data[,j], main=city, xlab="Month", ylab=paste("Data", j-1))
    }
}
par(mfrow=c(1, 1))

条形图

于 2012-07-18T19:44:28.790 回答
2

这是我的评论中提到的功能的略微修改版本。

read.funkyfile = function(funkyfile, expression, ...) {
  temp = readLines(funkyfile)
  temp.loc = grep(expression, temp)
  temp.loc = c(temp.loc, length(temp)+1)
  temp.nam = gsub("[[:punct:]][[:space:]]", "", 
                  grep(expression, temp, value=TRUE))
  temp.nam = gsub(expression, "", temp.nam)
  temp.out = vector("list")

  for (i in 1:length(temp.nam)) {
    temp.out[[i]] = read.table(textConnection(
      temp[seq(from = temp.loc[i]+1,
               to = temp.loc[i+1]-1)]),
                             ...)
    names(temp.out)[i] = temp.nam[i]
  }
  temp.out
}

假设您的文件名为“File.txt”,加载函数并像这样读取数据。您可以添加任何read.table您需要的参数:

temp = read.funkyfile("File.txt", "Header", header=TRUE, sep="\t")

现在,情节:

# to plot everything on one page (used for this example), uncomment the next line
# par(mfcol = c(length(temp), 1)) 
lapply(names(temp), function(x) barplot(as.matrix(temp[[x]][-1]), 
                                        beside=TRUE, main=x, 
                                        legend=TRUE))
# dev.off() or par(mfcol = c(1, 1)) if par was modified

这是您的小样本数据的样子par(mfcol = c(length(temp), 1))

在此处输入图像描述

于 2012-07-19T06:46:54.153 回答