
Untitled
By: a guest on
May 27th, 2012 | syntax:
None | size: 1.08 KB | hits: 21 | expires: Never
How to read a text file into GNU R with a multiple-byte separator?
1sep2sep3
1sep2sep3
1sep2sep3
1sep2sep3
1sep2sep3
1sep2sep3
1sep2sep3
dat <- readLines('test.csv')
dat <- gsub("sep", " ", dat)
dat <- textConnection(dat)
dat <- read.table(dat)
1sep2 2sep3
1sep2 2sep3
1sep2 2sep3
1sep2 2sep3
1sep2 2sep3
1sep2 2sep3
1sep2 2sep3
readMulti <- function(x, sep, replace, as.is = T)
{
dat <- readLines(x)
dat <- gsub(sep, replace, dat)
dat <- textConnection(dat)
dat <- read.table(dat, sep = replace, as.is = as.is)
return(dat)
}
readMulti('test.csv', sep = "sep", replace = "t", as.is = T)
> read.multisep <- function(File,sep) {
+ Lines <- readLines(File)
+ Matrix <- do.call(rbind,strsplit(Lines,sep,fixed=TRUE))
+ DataFrame <- structure(data.frame(Matrix[-1,]),names=Matrix[1,]) ## assuming header is present
+ DataFrame[] <- lapply(DataFrame,type.convert) ## automatically convert modes
+ DataFrame
+ }
>
> example <- "a#*&b#*&c
+ 1#*&2#*&3
+ 4#*&5#*&6"
>
> read.multisep(textConnection(example),sep="#*&")
a b c
1 1 2 3
2 4 5 6