繁体   English   中英

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

[英]Read a text file with tab and semicolon in R

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

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"

为什么不首先用分号替换选项卡(或多空格),然后正常导入:

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

假设我们有测试数据:

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.patterngsubfn包的开发版本中有一个新函数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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM