Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 27th, 2012  |  syntax: None  |  size: 1.08 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to read a text file into GNU R with a multiple-byte separator?
  2. 1sep2sep3
  3. 1sep2sep3
  4. 1sep2sep3
  5. 1sep2sep3
  6. 1sep2sep3
  7. 1sep2sep3
  8. 1sep2sep3
  9.        
  10. dat <- readLines('test.csv')
  11. dat <- gsub("sep", " ", dat)
  12. dat <- textConnection(dat)
  13. dat <- read.table(dat)
  14.        
  15. 1sep2 2sep3
  16. 1sep2 2sep3
  17. 1sep2 2sep3
  18. 1sep2 2sep3
  19. 1sep2 2sep3
  20. 1sep2 2sep3
  21. 1sep2 2sep3
  22.        
  23. readMulti <- function(x, sep, replace, as.is = T)
  24. {
  25.     dat <- readLines(x)
  26.     dat <- gsub(sep, replace, dat)
  27.     dat <- textConnection(dat)
  28.     dat <- read.table(dat, sep = replace, as.is = as.is)
  29.  
  30.     return(dat)
  31. }
  32.        
  33. readMulti('test.csv', sep = "sep", replace = "t", as.is = T)
  34.        
  35. > read.multisep <- function(File,sep) {
  36. +   Lines <- readLines(File)
  37. +   Matrix <- do.call(rbind,strsplit(Lines,sep,fixed=TRUE))
  38. +   DataFrame <- structure(data.frame(Matrix[-1,]),names=Matrix[1,]) ## assuming header is present
  39. +   DataFrame[] <- lapply(DataFrame,type.convert)                    ## automatically convert modes
  40. +   DataFrame
  41. + }
  42. >
  43. > example <- "a#*&b#*&c
  44. + 1#*&2#*&3
  45. + 4#*&5#*&6"
  46. >
  47. > read.multisep(textConnection(example),sep="#*&")
  48.   a b c
  49. 1 1 2 3
  50. 2 4 5 6