-1

I have a dataset from a text file that has simply 2 columns but multiple section breaks in data, that I want to place into separate arrays, where the name of the array is the text in the 2nd column adjacent to "Ran:". Below is an sample dataset:

ABCDEFG
Authored by test
Ran:    Efg$
Test:   num85
1       50
2       52
3       54
Ran:    pg2
Test:   num85
1       40
2       60
3       80
Ran:    #2
Test:   num85
1       14
2       15
3       16

I've tried using the strsplit function as follows:

 header = readLines("C:/My Documents/DVH Test.txt", n=17)
 data = read.table("C:/My Documents/DVH Test.txt", skip=16,
col.names = c("bin", "value"))

 data.split = strsplit(data, "R")

I'm not sure if I'm even using the right approach.

Any suggestions would be appreciated.

Thanks in advance.

Okay, I've tried this, but I'm getting an empty vector and the elements don't line up like yours:

 data = scan("C:/My Documents/DV.txt", what="raw")

 dat = readLines(textConnection(data))
 dat = dat[!grepl("Ran",dat)]
 dat.split = lapply(split(dat,cumsum(grepl("Test:",dat))),
     function(x)
         read.table(text=x,header=TRUE))
crazian
  • 649
  • 4
  • 12
  • 24

1 Answers1

1

Try this for example:

txt ='Ran:    Efg$
Test:   num85
1       50
2       52
3       54
Ran:    pg2
Test:   num85
1       40
2       60
3       80
Ran:    #2
Test:   num85
1       14
2       15
3       16'
## read all lines
ll <- readLines(textConnection(txt))
## remove "Ran"'s lines
ll <- ll[!grepl('Ran',ll)]
## split list in each headr an read it using 
## read.table(text=...)
lapply(split(ll,cumsum(grepl("Test:",ll))),
       function(x)
       read.table(text=x,header=TRUE))

which gives this list of data.frame's:

$`1`
  Test. num85
1     1    50
2     2    52
3     3    54

$`2`
  Test. num85
1     1    40
2     2    60
3     3    80

$`3`
  Test. num85
1     1    14
2     2    15
3     3    16
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • Hi, thanks for your solution, but I couldn't seem to get the elements to line up and I got an empty vector. I made some edits above. Any suggestions? – crazian Apr 01 '14 at 18:45