使用R中的制表符和分号读取文本文件

时间:2014-02-17 16:25:08

标签: r import read.table

我正在尝试导入包含信息的文本文件,如下所示:

Date (Tab) ON/OFF (Tab) 93489;123985;219389;1324;2349
Date (Tab) ON/OFF (Tab) 34536;34566;12346;235346;32567
Date (Tab) ON/OFF (Tab) 6346;235;6547457;2345;4576782

所以我将read.table()read.csv()与参数sep=";"sep="\t"一起使用。 他们工作,但不是两个在一起。有没有更好的方法来导入这些数据?阅读同时使用制表符和分号的文件?

最后,我需要提取的值,但此刻,这是不可能的,因为我得到这样的东西(包括“)

"18315;18316;18317;18318;18319;18320;18708"
"20317;20318;20319;20320;20321;20322;20714"

我可以替换“;”使用“”,但它仍然是一个字符串而strsplit()将不起作用...

"85626"
"81657 83249"
"84165 84560 84561 84957 85351 85746 85747 85748 86143"
"77701 78097 78893 86148 86149 86150"

2 个答案:

答案 0 :(得分:2)

为什么不首先用分号替换制表符(或多空格)然后正常导入:

tx<-"Date  ON/OFF  93489;123985;219389;1324;2349
Date  ON/OFF  34536;34566;12346;235346;32567
Date  ON/OFF  6346;235;6547457;2345;4576782"

read.table(text=gsub("([ /t]){2,9}",";",tx),header=F,sep=";")

    V1     V2    V3     V4      V5     V6      V7
1 Date ON/OFF 93489 123985  219389   1324    2349
2 Date ON/OFF 34536  34566   12346 235346   32567
3 Date ON/OFF  6346    235 6547457   2345 4576782

这是一个两步版本,用于处理不规则的分离项目数量:

df<-read.table(text=tx,header=F,stringsAsFactors=F)    # read table with ;-sep chars as one col

x.list<-strsplit(df[,ncol(df)],";")                    # turn the last row into a list, split by ;
max.length<-max(sapply(x.list,length))                 # work out the max length

cbind(df[,1:ncol(df)-1],                               # bind the first columns
  t(                                                   # to the transposed matrix
    sapply(x.list,function(x){length(x)<-max.length    # of the list, with each element expanded
                              x})                      # to max.length items (NAs for missing)
  )
)

    V1     V2     1      2       3      4       5     6
1 Date ON/OFF 93489 123985  219389   1324    2349  <NA>
2 Date ON/OFF 34536  34566   12346 235346   32567  <NA>
3 Date ON/OFF  6346    235 6547457   2345 4576782 43455

答案 1 :(得分:1)

假设我们有测试数据:

Lines <- "Date\tON/OFF\t93489;123985;219389;1324;2349
Date\tON/OFF\t34536;34566;12346;235346;32567
Date\tON/OFF\t6346;235;6547457;2345;4576782
"

我们将使用它来达到再现性的目的,但实际上你会使用类似注释掉的行:

1)read.table 使用制表符分隔符读取数据,然后使用分号分隔符重新读取第三列。最后结合它们:

# d1 <- read.table("myfile", as.is = TRUE)
d1 <- read.table(text = Lines, as.is = TRUE)
d2 <- read.table(text = d1[[3]], sep = ";")
d <- cbind(d1[1:2], d2)

,并提供:

    V1     V2    V1     V2      V3     V4      V5
1 Date ON/OFF 93489 123985  219389   1324    2349
2 Date ON/OFF 34536  34566   12346 235346   32567
3 Date ON/OFF  6346    235 6547457   2345 4576782

2)read.pattern gsubfn软件包的开发版本中有一个新函数read.pattern,这使得这很简单:

library(gsubfn)
source("http://gsubfn.googlecode.com/svn/trunk/R/read.pattern.R")

# read.pattern("myfile", pattern = "[^[:space:];]+")
read.pattern(text = Lines, pattern = "[^[:space:];]+")

,并提供:

    V1 V2  V3    V4     V5      V6     V7      V8
1 Date ON OFF 93489 123985  219389   1324    2349
2 Date ON OFF 34536  34566   12346 235346   32567
3 Date ON OFF  6346    235 6547457   2345 4576782

已修订在第二个解决方案中,在模式参数中更改了正则表达式,并在https语句中将http更改为source