当前位置: 动力学知识库 > 问答 > 编程问答 >

transformation - Read rows with different Width and several headers in R

问题描述:

I have the following log that I need to read in R

MO RSITE COMB FHOP MODEL

RXOTG-0 3BFR42 HYB SY G12

SWVERREPL SWVERDLD SWVERACT TMODE

B1312R078G B1312R078G SCM

MO RSITE COMB FHOP MODEL

RXOTG-1 3BFR42 HYB SY G12

SWVERREPL SWVERDLD SWVERACT TMODE

B1312R078G B1312R078G SCM

I need to read like that

MO RSITE COMB FHOP MODEL SWVERREPL SWVERDLD SWVERACT TMODE

RXOTG-0 3BFR42 HYB SY G12 B1312R078G B1312R078G SCM

RXOTG-1 3BFR42 HYB SY G12 B1312R078G B1312R078G SCM

I can't use read.fwf because each row has a differente width and every row has a header. The only way I could read is using read.table sep = " " and after heavy transformations I had the output wanted.

Someone has any suggestion to read with less coding?

网友答案:

You can get some of the way by using readLines and then extracting lines, pasting them together and using the text argument in read.table to import the data.

f <- readLines("myFile.txt")
header  <- paste(f[1], f[3])
body <- sapply(seq(1, length(f), 6), function(i) paste(f[i+1], f[i+4]))

read.table(header = FALSE, text = paste(body, collapse = "\n"))

This omits the blank column. If you need this, or different columns are blank, use read.fwf instead.

网友答案:

This will read your sample but will fail if any of the entries have unquoted embedded white space:

data.frame( scan(text=txt, 
                 what=list( MO="", RSITE="", COMB="",  FHOP="", 
                            MODEL="", SWVERREPL="",   SWVERDLD="", 
                            SWVERACT ="",   TMODE=""), 
                 multi.line=TRUE))[c(TRUE, FALSE), ] # removes extraneous extra "headers"
#--------------
Read 4 records
  MO RSITE COMB FHOP MODEL SWVERREPL SWVERDLD SWVERACT TMODE
1 MO RSITE COMB FHOP MODEL   RXOTG-0   3BFR42      HYB    SY
3 MO RSITE COMB FHOP MODEL   RXOTG-1   3BFR42      HYB    SY
分享给朋友:
您可能感兴趣的文章:
随机阅读: