[R] How to read a list into R??

Aaron McDaid aaronmcdaid at gmail.com
Tue Jun 30 09:38:07 CEST 2009


Hi,
You should not use the 'sink' function to save these data files. If
you want a readable format, you should look at 'dump' instead. If you
simply want to save your data structures then 'save' might be best.

'sink' is not appropriate for data saving. It's simply a convenient
way to log what you see in the terminal.

Aaron

On Mon, Jun 29, 2009 at 23:58, Li,Hua<huali at 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 at 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.
>




More information about the R-help mailing list