-5

I want to read a file that contain many vector for example you can see below

8984
8813
8685
11629
c(8527, 11629)
c(8527, 7685, 7822, 11629)
c(8527, 7685, 7822, 7137, 7318, 11629)
c(8527, 7685, 7822, 7137, 7318, 7063, 7075, 11629)
c(8527, 7685, 7822, 7137, 7318, 7063, 7075, 6911, 6946, 11629)
c(8527, 7685, 7822, 7137, 7318, 7063, 7075, 6911, 6946, 6703, 6909, 11629)
c(8527, 7685, 7822, 7137, 7318, 7063, 7075, 6911, 6946, 6703, 6909, 5751, 6614, 11629)
c(8527, 7685, 7822, 7137, 7318, 7063, 7075, 6911, 6946, 6703, 6909, 5751, 6614, 5436, 5493, 11629)
c(8527, 7685, 7822, 7137, 7318, 7063, 7075, 6911, 6946, 6703, 6909, 5751, 6614, 5436, 5493, 4694, 4998, 11629)
c(8527, 7685, 7822, 7137, 7318, 7063, 7075, 6911, 6946, 6703, 6909, 5751, 6614, 5436, 5493, 4694, 4998, 4211, 4678, 11629)

how can I read this file that every vector is specific in R?

smci
  • 32,567
  • 20
  • 113
  • 146
  • 3
    What steps have you tried so far that haven't worked? – hrbrmstr Oct 16 '14 at 18:44
  • `lapply(readLines("vectors.dat"), function(x) { eval(parse(text=x)) })` will read in a file of R expressions, parse & evaluate them. This gives a list back, you can `unlist` them if you just want all the values. alternately `eval(parse("vectors.dat"))` but that will provide a different outcome (just the last line). – hrbrmstr Oct 16 '14 at 18:51
  • @hrbrmstr that line of code looks like a security nightmare – ilir Oct 16 '14 at 20:08
  • it is a security nightmare :-) but when you've got a bunch of vectors to read in (and, in theory, control the data) it shouldn't be an issue. – hrbrmstr Oct 16 '14 at 20:58
  • cran.r-project.org/web/packages/RAppArmor/vignettes/v55i07.pdf ... – IRTFM Oct 16 '14 at 21:53

1 Answers1

2

If that's really what your file looks like (which would be strange), give this a try.

It works when I tried it on textConnection(yourtext) so it should work on your file. You don't tell us how you want the output to look, so I made it a list because that seems most appropriate here.

txt <- gsub("[c(),]", "", readLines("filename.ext"))
lapply(txt, function(x) scan(text = x, what = integer(), quiet = TRUE))
# [[1]]
# [1] 8984
# 
# [[2]]
# [1] 8813
# 
# [[3]]
# [1] 8685
# 
# [[4]]
# [1] 11629
# 
# [[5]]
# [1]  8527 11629
# 
# [[6]]
# [1]  8527  7685  7822 11629
#
# ... truncated ...
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245