Hi,

It would be much better to save your list with dump or dput or save (then
you can read it, respectively, with source, dget, or load). Sink is not
useful for this, but if you really have to (i.e., if you for some reason
can't re-run the analyses and make these lists anew), you can do something
like the following. (It works for your example, but you need something more
complicated for nested and/or named lists.)

xx <- scan('List.txt', what=character(0))
# I presume List.txt is exactly the same as your pasted example

foo <- which(substr(xx,1,2)=="[[")
xx2 <- split(xx, do.call(c,mapply(rep, 1:length(foo), diff(c(foo,
length(xx)+1)))))
# this just divides your vector into 3 pieces (using the fields starting
with [[ as delimiters)

xx2 <- lapply(xx2, function(x) x[-which(substr(x,1,1)=="[")])
# this throws away anything starting with [, i.e., keeps only data

xx2 <- lapply(xx2, as.numeric)
# to convert your data to numeric

names(xx2) <- NULL
# deletes names -- there were none in the original list

(And obviously, for more complicated lists this strategy is probably
unreasonably complicated, so you might still want to redo your analyses
rather than reconstructing the results.)

Have a good day,

Kenn

On Tue, Jun 30, 2009 at 1:58 AM, Li,Hua <huali@mdanderson.org> wrote:

> Dear R helpers:
>      I have tried many times to find some way to read a list into R. But I
> faid. Here is an example:
>      I have a file 'List.txt' which includes data as follows:
> [[1]]
>  [1] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.5 0.0 0.0
> 0.0
>  [19] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
>
> [[2]]
>  [1] 0.0000000 0.5000000 0.0000000 0.0000000 0.5000000 0.0000000 0.0000000
>  [8] 0.0000000 0.0000000 0.0000000
>
> [[3]]
>  [1] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.5 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
> 0.0
>  [19] 0.0 0.0 0.0 0.0 0.5 0.0 0.0 0.5 0.0 0.5 0.0 0.0 0.0 0.0
>
> 'List.txt' was output by 'sink' from R.
> I try to read 'List.txt' into R.
> First I tried 'dget', I got
> > dget('Vlist300.txt')
> Error in parse(file = file) : Vlist300.txt: unexpected '[[' at
> 1: [[
>
> Then I tried 'scan',
> >scan('List.txt', what='list')
> Read 86 items
>  [1] "[[1]]"     "[1]"       "0.0"       "0.0"       "0.0"       "0.0"
>  [7] "0.0"       "0.0"       "0.0"       "0.0"       "0.0"       "0.0"
> [13] "0.0"       "0.0"       "0.0"       "0.0"       "0.5"       "0.0"
> [19] "0.0"       "0.0"       "[19]"      "0.0"       "0.0"       "0.0"
> [25] "0.0"       "0.0"       "0.0"       "0.0"       "0.0"       "0.0"
> [31] "0.0"       "0.0"       "0.0"       "0.0"       "0.0"       "0.0"
> [37] "0.0"       "0.0"       "[[2]]"     "[1]"       "0.0000000"
> "0.5000000"
> [43] "0.0000000" "0.0000000" "0.5000000" "0.0000000" "0.0000000" "[8]"
> [49] "0.0000000" "0.0000000" "0.0000000" "[[3]]"     "[1]"       "0.0"
> [55] "0.0"       "0.0"       "0.0"       "0.0"       "0.0"       "0.0"
> [61] "0.0"       "0.5"       "0.0"       "0.0"       "0.0"       "0.0"
> [67] "0.0"       "0.0"       "0.0"       "0.0"       "0.0"       "[19]"
> [73] "0.0"       "0.0"       "0.0"       "0.0"       "0.5"       "0.0"
> [79] "0.0"       "0.5"       "0.0"       "0.5"       "0.0"       "0.0"
> [85] "0.0"       "0.0"
>
> Unfortunately I can't find any function to read 'List.txt' into R and to
> give me the right format as in List.txt. Do you know if there's a function
> that can read 'List.txt' into R and keep the format as follows?
>
> [[1]]
>  [1] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.5 0.0 0.0
> 0.0
>  [19] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
>
> [[2]]
>  [1] 0.0000000 0.5000000 0.0000000 0.0000000 0.5000000 0.0000000 0.0000000
>  [8] 0.0000000 0.0000000 0.0000000
>
> [[3]]
>  [1] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.5 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
> 0.0
>  [19] 0.0 0.0 0.0 0.0 0.5 0.0 0.0 0.5 0.0 0.5 0.0 0.0 0.0 0.0
>
>  I appreciate any help!!
> Best,
> Hua
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]


