2

这个文件为例,我正在尝试读取 data.frame 中的数据。从文档(pdf 文件,表 1)中,它遵循一些 fortran 约定。我尝试了以下但收效甚微:

dir <- "Uncompressed-files/"
file <- "01_hit09.par"

delim <- c("I2", "I1", "F12.6", "E10.3", "E10.3", "F5.4", "F5.4", 
"F10.4", "F4.2", "F8.6", "A15", "A15", "A15", "A15", "6I1", "6I2", 
"A1", "F7.1", "F7.1")
test <- read.fortran(paste0(dir, file), delim)

test <- read.fwf(paste0(dir, file),
                 c(2, 1, 12, 10, 10, 5, 5, 10, 4, 8,
                   15, 15, 15, 15, 6, 12, 1, 7, 7))

指定正确格式的任何提示?

4

1 回答 1

4

根据?read.fortran

  1. "E"不是受支持的格式。
  2. “在 'F' 或 'D' 格式中指定 'd > 0' 会将小数点 'd' 向左移动,即使它在输入文件中明确指定。”

因此,更改"E""F"删除所有显式十进制格式:

file <- "01_hit09.par"
delim <- c("I2", "I1", "F12", "F10", "F10", "F5", "F5", "F10", "F4", "F8",
           "A15", "A15", "A15", "A15", "6I1", "6I2", "A1", "F7", "F7")
(test <- read.fortran(file, delim, n=10))

更新:或正确指定宽度并使用read.fwf

 test2 <- read.fwf(file, c(2,1,12,10,10,5,5,10,4,8,15,15,15,15,
                           rep(1,6),rep(2,6),1,7,7), as.is=TRUE)
于 2012-06-18T22:41:42.177 回答