0

I am working with R HMM package. I have a file with symbols on each line. I need to create 2D vector from this file, where i-th vector contains symbols from i-th line

I wrote code likes:

q4 = readLines("someFile")
c1 <- strsplit(q4[1],"")[[1]]
c2 <- strsplit(q4[2],"")[[1]]
c3 <- strsplit(q4[3],"")[[1]]
baumWelch(hmm, c(c1,c2,c3), 10)

and it works. But I have 50000 lines, so I need to do it in loop. So I wrote this one:

q4 = readLines("someFile")
results <- vector(length=length(q4))
for (i in 1:length(q4)) {
   results[i] <- strsplit(q4[i],"")[[1]]
}
baumWelch(hmm, results, 10)

But each vectors contained only first symbol from the line. So I removed

[[1]]

from code, but type of results was list and not vector so I can not use it. How can I do it correctly?

Marek Čačko
  • 980
  • 2
  • 21
  • 31
  • Why you evaluate `results` while you are using `c(c1,c2,c3)` in the call of `baumWelch`? Also, `strsplit` is vectorized, so no need of a loop. Don't know the package, but I guess that you are looking for `baumWelch(hmm, do.call(rbind,strsplit(q4,"")),10)`. – nicola Mar 29 '15 at 20:29
  • I copied and pasted this line and forgot to change it. Now it is fixed. Your code has a problem with: number of columns of result is not a multiple of vector length (arg 1) – Marek Čačko Mar 29 '15 at 20:41
  • Does every line have the same number of elements? Are you sure you want to pass a `matrix` object (which is a 2D array in R) or you need a `list`? Do you know the difference? – nicola Mar 29 '15 at 20:50

0 Answers0