Question

I'm trying to import a text file containing informations as follows:

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

So I use the read.table() or read.csv() with the argument sep=";" or sep="\t". They work, but not both together. Is there a better way to import these data? Read a file that uses both tabs and semicolon?

Finally, I need the extracted values, but at the moment, that is not possible, because I get something like this (including the " )

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

I can replace the ";" with " ", but it is still a string and strsplit() won't work...

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

Solution

Why not first replace the tab (or multi-space) with a semi-colon then import as normal:

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

Here's a 2 step version to deal with the number of ;-separated items being irregular:

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

OTHER TIPS

Suppose we have the test data:

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
"

We will use this for the purpose of reproducibility but in reality you would use something like the commented out lines:

1) read.table Read the data with a tab separator and then re-read the third column using a semicolon separator. Finally combine them:

# 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)

giving:

    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 There is a new function read.pattern in the development version of the gsubfn package that makes this simple to do:

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

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

giving:

    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

REVISED In second solution changed regular expression in pattern argument and changed https to http in source statement.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top