@thelatemail は、続行する方法についてスポットオンです。より堅牢なソリューションを開始するためにまとめた小さな関数を次に示します。
read.dat.dct <- function(dat, dct) {
temp <- readLines(dct)
pattern <- "_column\\(([0-9]+)\\)\\s+([a-z0-9]+)\\s+([a-z0-9_]+)\\s+%([0-9]+).*"
classes <- c("numeric", "character", "character", "numeric")
metadata <- setNames(lapply(1:4, function(x) {
out <- gsub(pattern, paste("\\", x, sep = ""), temp)
out <- gsub("^\\s+|\\s+$|.*\\{|\\}", "", out)
out <- out[out != ""]
class(out) <- classes[x] ; out }),
c("StartPos", "Str", "ColName", "ColWidth"))
read.fwf(dat, widths = metadata[["ColWidth"]],
col.names = metadata[["ColName"]])
}
エラーチェック、関数の一般化などに関して、やらなければならないことはまだたくさんあります。たとえば、この関数は、@thelatemail が質問に追加した例にあるように、重複する列では機能しません。"StartPos[n] + ColWidth[n]" は "StartPos[n+1]" と等しくなければならないという形式のエラー チェックを使用して、エラー メッセージが表示されてそうでない場合、ファイルの読み取りを停止できます。さらに、結果のデータのクラスは、関数によって生成されread.fwf
、引数を使用して割り当てられた「メタデータ」リストから抽出することもできcolClasses
ます。
デモ用の dat ファイルと dct ファイルを次に示します。
次の 2 行をコピーしてテキスト エディターに貼り付け、作業ディレクトリに "test.dat" として保存します。
C1245A101George Costanza
B1223B011Cosmo Kramer
次の行をコピーしてテキスト エディターに貼り付け、作業ディレクトリに「test.dct」として保存します。
dictionary using test.dat {
_column(1) str1 code %1s
_column(2) int call %4f
_column(6) str1 city %1s
_column(7) int neigh %3f
_column(10) str16 name %16s
}
次に、関数を実行します。
read.dat.dct(dat = "test.dat", dct = "test.dct")
# code call city neigh name
# 1 C 1245 A 101 George Costanza
# 2 B 1223 B 11 Cosmo Kramer
更新: 改善された機能 (まだ改善の余地がたくさんあります)
read.dat.dct <- function(dat, dct, labels.included = "no") {
temp <- readLines(dct)
temp <- temp[grepl("_column", temp)]
switch(labels.included,
yes = {
pattern <- "_column\\(([0-9]+)\\)\\s+([a-z0-9]+)\\s+(.*)\\s+%([0-9]+)[a-z]\\s+(.*)"
classes <- c("numeric", "character", "character", "numeric", "character")
N <- 5
NAMES <- c("StartPos", "Str", "ColName", "ColWidth", "ColLabel")
},
no = {
pattern <- "_column\\(([0-9]+)\\)\\s+([a-z0-9]+)\\s+(.*)\\s+%([0-9]+).*"
classes <- c("numeric", "character", "character", "numeric")
N <- 4
NAMES <- c("StartPos", "Str", "ColName", "ColWidth")
})
metadata <- setNames(lapply(1:N, function(x) {
out <- gsub(pattern, paste("\\", x, sep = ""), temp)
out <- gsub("^\\s+|\\s+$", "", out)
out <- gsub('\"', "", out, fixed = TRUE)
class(out) <- classes[x] ; out }), NAMES)
metadata[["ColName"]] <- make.names(gsub("\\s", "", metadata[["ColName"]]))
myDF <- read.fwf(dat, widths = metadata[["ColWidth"]],
col.names = metadata[["ColName"]])
if (labels.included == "yes") {
attr(myDF, "col.label") <- metadata[["ColLabel"]]
}
myDF
}
それはあなたのデータでどのように機能しますか?
temp <- read.dat.dct(dat = "http://dl.getdropbox.com/u/18116710/21600-0009-Data.txt",
dct = "http://dl.getdropbox.com/u/18116710/21600-0009-Setup.dct",
labels.included = "yes")
dim(temp) # How big is the dataset?
# [1] 180 40
head(temp[, 1:6]) # What do the first few columns & rows look like?
# CASEID AID RRELNO RPREGNO H3PC1.H3PC1 H3PC2.H3PC2
# 1 1 57118381 5 1 1 1
# 2 2 57134970 1 2 1 1
# 3 3 57135078 1 1 1 1
# 4 4 57135078 5 1 1 1
# 5 5 57164981 1 1 7 3
# 6 6 57191909 1 3 1 1
head(attr(temp, "col.label")) # What are the variable labels?
# [1] "CASE IDENTIFICATION NUMBER" "RESPONDENT IDENTIFIER"
# [3] "ROMANTIC RELATIONSHIP NUMBER" "RELATIONSHIP PREGNANCY NUMBER"
# [5] "S23Q1 1 TOLD PARTNER PREGNANT-W3" "S23Q2 MONTHS PREG WHEN TOLD PARTNER-W3"
元の例ではどうですか?
read.dat.dct("test.dat", "test.dct", labels.included = "no")
# code call city neigh name
# 1 C 1245 A 101 George Costanza
# 2 B 1223 B 11 Cosmo Kramer