是否有使用Stata .do脚本将固定宽度数据导入R的简单方法?

时间:2019-02-26 20:38:11

标签: r import cross-platform stata fixed-width

我有一个Stata .do脚本文件,该文件用于从固定宽度的TXT文件导入数据。 .do文件如下所示:

#delimit ;

**************************************************************************
   Label           : CDS 2014 ID Map
   Rows            : 4353
   Columns         : 7
   ASCII File Date : December 11, 2017
*************************************************************************;


infix 
      CHLDID14        1 - 5         CHLDSN14        6 - 7         PCGID14         8 - 12   
      PCGSN14        13 - 14        CDSHID14       15 - 18        CHLDINST14     19 - 20   
      PCGINST14      21 - 22   
using [path]\IDMAP14.txt, clear 
;
label variable  CHLDID14     "CHILD 2013 PSID FAMILY IW (ID) NUMBER" ;           
label variable  CHLDSN14     "CHILD 2013 INDIVIDUAL SEQUENCE NUMBER" ;           
label variable  PCGID14      "PCG 2013 PSID FAMILY IW (ID) NUMBER" ;             
label variable  PCGSN14      "PCG 2013 INDIVIDUAL SEQUENCE NUMBER" ;             
label variable  CDSHID14     "CDS 2014 HOUSEHOLD INTERVIEW NUMBER" ;             
label variable  CHLDINST14   "CDS 2014 HH ROSTER CHILD SEQUENCE NUM" ;           
label variable  PCGINST14    "CDS 2014 HH ROSTER PCG SEQUENCE NUM" ;             

是否有使用此.do文件自动将数据导入R的快速方法?还是我必须使用列范围来手动调整脚本?

我问是因为我只能访问R(而不是Stata),但是Stata .do文件似乎是将数据正确导入R的最简单快捷方式。

谢谢!

链接到文件:Fixed-width text fileStata .do script

1 个答案:

答案 0 :(得分:3)

这是一个刺,但是由于我们没有可用于验证的文件,因此您需要自己承担。我对此格式做出的许多假设可能需要验证,即:

  • 我们在具有文字infix和文字using的行之间找到列定义
  • 每个列定义正好是columnname from hyphen to,并带有空格(即使是单个字符,也将是somename 5 - 5
  • 文件名紧跟在文字using之后;末尾逗号后面可以跟clear或其他非逗号字符,而不是文件名的一部分
do2fwf <- function(txt) {

  infix <- grep("^infix\\s*", txt) 
  if (length(infix) != 1L) stop("need exactly one 'infix' line")
  using <- grep("^\\s*using\\b", txt)
  if (length(using) != 1L) stop("need exactly one 'using' line")
  if (using < infix) stop("'infix' must occur before 'using'")

  hdrtxt <- txt[ (infix+1):(using-1) ]
  # "      CHLDID14        1 - 5         CHLDSN14        6 - 7         PCGID14         8 - 12   "

  re <- gregexpr("\\S+", hdrtxt)
  m <- regmatches(hdrtxt, re)
  # [[1]]
  #  [1] "CHLDID14" "1"        "-"        "5"        "CHLDSN14" "6"        "-"        "7"        "PCGID14"  "8"       
  # [11] "-"        "12"      

  if (!all(lengths(m) %% 4 == 0))
    stop("not all variables are the right format of 'name i - j'")
  if (any(lengths(m) == 0)) {
    warning("found empty lines, confusing")
    m <- Filter(length, m)
  }

  # need to convert 4x lists into 1x lists
  m2 <- do.call("c", mapply(split, m, lapply(lengths(m), function(a) (1:a-1) %/% 4)))

  nms <- sapply(m2, `[[`, 1)

  froms <- as.integer(sapply(m2, `[[`, 2))
  tos <- as.integer(sapply(m2, `[[`, 4))
  widths <- tos - froms + 1

  filename <- gsub("^\\s*using\\s*", "", txt[using])
  # this works here, but I don't know if it is generic and rule-following
  filename <- gsub("\\s*,[^,]*$", "", filename)

  list(filename = filename, names = nms, widths = widths)
  # x <- read.fwf(filename, widths=widths, ...) # header=FALSE???
  # colnames(x) <- names
}

如果您使用底部的数据(实际上应该只是txt <- readLines("somefile.do"),则会得到以下信息:

do2fwf(txt)
# $filename
# [1] "[path]\\IDMAP14.txt"
# $names
#            0            1            2            0            1            2            0 
#   "CHLDID14"   "CHLDSN14"    "PCGID14"    "PCGSN14"   "CDSHID14" "CHLDINST14"  "PCGINST14" 
# $widths
# [1] 5 2 5 2 4 2 2

您可以自己使用(根据评论)。我不知道您可能需要read.fwf的标题行或其他参数。祝你好运!


文字:

txt <- readLines(textConnection('**************************************************************************
   Label           : CDS 2014 ID Map
   Rows            : 4353
   Columns         : 7
   ASCII File Date : December 11, 2017
*************************************************************************;


infix 
      CHLDID14        1 - 5         CHLDSN14        6 - 7         PCGID14         8 - 12   
      PCGSN14        13 - 14        CDSHID14       15 - 18        CHLDINST14     19 - 20   
      PCGINST14      21 - 22   
using [path]\\IDMAP14.txt, clear 
;
label variable  CHLDID14     "CHILD 2013 PSID FAMILY IW (ID) NUMBER" ;           
label variable  CHLDSN14     "CHILD 2013 INDIVIDUAL SEQUENCE NUMBER" ;           
label variable  PCGID14      "PCG 2013 PSID FAMILY IW (ID) NUMBER" ;             
label variable  PCGSN14      "PCG 2013 INDIVIDUAL SEQUENCE NUMBER" ;             
label variable  CDSHID14     "CDS 2014 HOUSEHOLD INTERVIEW NUMBER" ;             
label variable  CHLDINST14   "CDS 2014 HH ROSTER CHILD SEQUENCE NUM" ;           
label variable  PCGINST14    "CDS 2014 HH ROSTER PCG SEQUENCE NUM" ;             '))