R - 使用.sas字典文件和SAScii导入ASCII数据

joh*_*fle 5 ascii r

我最近下载了一些ASCII格式的数据,这些数据附带SAS设置文件,我想与R一起使用.一个这样的数据文件在这里:

https://dl.dropboxusercontent.com/u/8474088/Data.txt

使用相应的SAS设置文件:

https://dl.dropboxusercontent.com/u/8474088/Setup.sas

我应该注意,安装文件设计用于处理大约50个具有相似结构的不同数据文件(上面的链接是其中一个的示例).

我认为在找到SAScii软件包后我状态良好,但是无法获得read.SAScii或parse.SAScii来处理这些文件.这两个命令都会出错.

read.SAScii(data.file,setup.file,beginline=581)

Error in if (as.numeric(x[j, "start"]) > as.numeric(x[j - 1, "end"]) +  : 
  missing value where TRUE/FALSE needed
In addition: Warning message:
NAs introduced by coercion 

parse.SAScii(setup.file,beginline=581)

Error in if (as.numeric(x[j, "start"]) > as.numeric(x[j - 1, "end"]) +  : 
  missing value where TRUE/FALSE needed
In addition: Warning message:
NAs introduced by coercion 
Run Code Online (Sandbox Code Playgroud)

SAScii文档中给出的示例使用了更简单的设置文件,因此我想知道上述文件的复杂性是否导致了问题(例如,在INPUT命令之前文件中列出的VALUE信息).

任何关于如何进行的想法都会很棒.提前致谢.

Ant*_*ico 4

如parse.SAScii 帮助的详细信息部分所述,该包无法读取重叠的列..并且您的文件显然有它们。;) 为了使 SAScii 工作,您必须将文件分成硬盘驱动器上的.sas四个单独的文件。.sas就是这样-

# load all necessary libraries
library(stringr)
library(SAScii)
library(downloader)

# create two temporary files
tf <- tempfile()
tf2 <- tempfile()

# download the sas import script
download( "https://dl.dropboxusercontent.com/u/8474088/Setup.sas" , tf )

# download the actual data file
download( "https://dl.dropboxusercontent.com/u/8474088/Data.txt" , tf2 )

# read the sas importation instructions into R
z <- readLines( tf )

# here are the break points
z[ substr( str_trim( z ) , 1 , 1 ) == '#' ]

sas.script.breakpoints <- which( substr( str_trim( z ) , 1 , 1 ) == '#' )

script.one <- z[ 581:sas.script.breakpoints[1] ]
script.two <- z[ sas.script.breakpoints[1]:sas.script.breakpoints[2] ]
script.three <- z[ sas.script.breakpoints[2]:sas.script.breakpoints[3] ]
script.four <- z[ sas.script.breakpoints[3]:length(z) ]

# replace some stuff so these look like recognizable sas scripts
script.one[ length( script.one ) ] <- ";"

script.two[ 1 ] <- "input blank 1-300"
script.two[ length( script.two ) ] <- ";"

script.three[ 1 ] <- "input blank 1-300"
script.three[ length( script.three ) ] <- ";"

script.four[ 1 ] <- "input blank 1-300"

# test then import data set one
writeLines( script.one , tf )
parse.SAScii( tf )
x1 <- read.SAScii( tf2 , tf )

# test then import data set two
writeLines( script.two , tf )
parse.SAScii( tf )
x2 <- read.SAScii( tf2 , tf )

# test then import data set one
writeLines( script.three , tf )
parse.SAScii( tf )
x3 <- read.SAScii( tf2 , tf )

# test then import data set four
writeLines( script.four , tf )
parse.SAScii( tf )
x4 <- read.SAScii( tf2 , tf )
Run Code Online (Sandbox Code Playgroud)