0

私はRを初めて使用し、異なるデータセット間にヘッダーを付けて1つのファイルから多くのグラフを生成しようとしています。次のようにフォーマットされたタブ区切りのプレーンテキストファイルがあります。

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では、各行を繰り返し処理し、行が「Header」で始まる場合は別のことを実行してから、何らかの方法で数値を処理できることを知っています。私は単にこれをしたいと思います:

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 に答える