Re: [R] read in data file into R

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Mon, 31 May 2010 10:06:28 -0400

On May 31, 2010, at 9:47 AM, Benedikt Gehr wrote:

> Hi
> I'm trying to read a data file with output from another program
> (admb) into R for further analysis. However I'm not very
> successfull. The file extension for the data file is file.rep but it
> also doesn't help when I change it to file.txt
>
> I have two problems/questions:
>
> 1. The file is a single line of n values separated by a single
> space tab each. These values represent a time series of length n.
> How can I make a numeric vector with this data?

?scan

You will also need to read about connections:

?connections

> When I use the "read.table" command and read in the file R produces
> a list of as many objects as there are values (n). However what I
> need is a vector of length n in order to work with the data. When I
> try to coerce the list into a single vector using "as.vector" this
> doesn't work.
> When I specify sep="\n" then I get a list where all the n values are
> treated as one value and I cant extract single values.
>
> 2. And related to the issue above: When I have a data file which
> consists of two objects, one is a matrix and the other one is a
> vector. Can I read the file into R all at once as a list with 2
> objects and then extract the matrix and the vector and work with
> them? Or is it necessary to first make two files, for each object one?

It would be much simpler to adopt the second strategy.

>
> Below I have copied a subset of my data files for ilustration. This
> seems a very silly question but I just didn't manage to to it.
> Thanks a lot for the help
>
> cheers
>
> beni
>
> This is a subset of my data file for 1.:
>
> Time series of reconstructed populations
> 3709.17 2660.93 2045.36 2090.33 2096.93 2205.65 2083.72 1797.53
> 1884.61 1946.59 2101.66 2220.03 2080.04 2097.07 2332.9 2325.47
> 2091.67 2091.54 2072.38 2025.31 1919.54 1781.95 1867.96 1685.12
> 1826.31 1654.25 1593.84 1430.96 1539.89 1587.35 1472.32 1737.02
> 1510.37 1570.15 1723.21 1755.3 1843.85 1829.2 1880.63 1916.79
> 1945.86 2096.64 2246.67 2101.16 2134.39 2018.1 2174.04 ....
>
> This is a subset of the data file for 2.:
> Reconstructed population
> 203.026 200.005 205.206 217.36 279.415 750.965
> 495.041 91.3615 162.004 147.748 156.499 492.444
> 463.284 222.768 74.0028 116.643 106.379 303.677
> 468.042 208.478 180.442 53.282 83.9828 194.375
> 460.216 210.619 168.867 129.918 38.3631 135.857
> 461.88 207.097 170.601 121.584 93.5413 80.3142
> 474.857 207.846 167.749 122.833 87.5406 98.5
> 479.117 213.686 168.355 120.779 88.4396 101.233
> 480.269 215.603 173.085 121.216 86.961 102.94
> 483.206 216.121 174.638 124.622 87.2753 102.538
> 486.657 217.443 175.058 125.739 89.7275 102.608
> 490.516 218.996 176.128 126.042 90.5324 104.401
> 494.019 220.732 177.386 126.813 90.7501 105.676
> 497.345 222.308 178.793 127.718 91.305 106.327
> 500.797 223.805 180.07 128.731 91.9571 106.979
> 504.331 225.359 181.282 129.65 92.6863 107.701
> 507.892 226.949 182.54 130.523 93.3482 108.507
> 511.458 228.551 183.829 131.429 93.9768 109.296
> 515.039 230.156 185.127 132.357 94.629 110.054
> 518.65 231.767 186.426 133.291 95.2967 110.818
> 522.291 233.393 187.732 134.227 95.9696 111.595
> 525.957 235.031 189.048 135.167 96.6434 112.381
> 529.648 236.681 190.375 136.115 97.32 113.171
> 533.364 238.342 191.711 137.07 98.0025 113.964
> 537.106 240.014 193.057 138.032 98.6905 114.763
> 541.61 241.698 194.411 139.001 99.3832 115.569
> 545.435 243.725 195.775 139.976 100.081 116.38
> 549.312 245.446 197.417 140.958 100.783 117.197
> 553.294 247.19 198.811 142.14 101.49 118.019
> 557.349 248.982 200.224 143.144 102.341 118.847
>
> time series of the reconstructed population
> 1855.98 1545.1 1286.75 1188.6 1143.84 1135.02 1159.33 1171.61
> 1180.07 1188.4 1197.23 1206.61 1215.38 1223.8 1232.34 1241.01
> 1249.76 1258.54 1267.36 1276.25 1285.21 1294.23 1303.31 1312.45
> 1321.66 1331.67 1341.37 1351.11 1360.94 1370.89

Pop1 <- scan(textConnection("3709.17 2660.93 2045.36 2090.33 2096.93

2205.65 2083.72 1797.53 1884.61 1946.59 2101.66 2220.03 2080.04  
2097.07 2332.9 2325.47 2091.67 2091.54 2072.38 2025.31 1919.54 1781.95  
1867.96 1685.12 1826.31 1654.25 1593.84 1430.96 1539.89 1587.35  
1472.32 1737.02 1510.37 1570.15 1723.21 1755.3 1843.85 1829.2 1880.63  
1916.79 1945.86 2096.64 2246.67 2101.16 2134.39 2018.1 2174.04 \n\n"))
#Read 47 items
Pop.2 <- scan(textConnection("203.026 200.005 205.206 217.36 279.415  
750.965
495.041 91.3615 162.004 147.748 156.499 492.444
463.284 222.768 74.0028 116.643 106.379 303.677
468.042 208.478 180.442 53.282 83.9828 194.375
460.216 210.619 168.867 129.918 38.3631 135.857
461.88 207.097 170.601 121.584 93.5413 80.3142
474.857 207.846 167.749 122.833 87.5406 98.5
479.117 213.686 168.355 120.779 88.4396 101.233
480.269 215.603 173.085 121.216 86.961 102.94
483.206 216.121 174.638 124.622 87.2753 102.538
486.657 217.443 175.058 125.739 89.7275 102.608
490.516 218.996 176.128 126.042 90.5324 104.401
494.019 220.732 177.386 126.813 90.7501 105.676
497.345 222.308 178.793 127.718 91.305 106.327
500.797 223.805 180.07 128.731 91.9571 106.979
504.331 225.359 181.282 129.65 92.6863 107.701
507.892 226.949 182.54 130.523 93.3482 108.507
511.458 228.551 183.829 131.429 93.9768 109.296
515.039 230.156 185.127 132.357 94.629 110.054
518.65 231.767 186.426 133.291 95.2967 110.818
522.291 233.393 187.732 134.227 95.9696 111.595
525.957 235.031 189.048 135.167 96.6434 112.381
529.648 236.681 190.375 136.115 97.32 113.171
533.364 238.342 191.711 137.07 98.0025 113.964
537.106 240.014 193.057 138.032 98.6905 114.763
541.61 241.698 194.411 139.001 99.3832 115.569
545.435 243.725 195.775 139.976 100.081 116.38
549.312 245.446 197.417 140.958 100.783 117.197
553.294 247.19 198.811 142.14 101.49 118.019
557.349 248.982 200.224 143.144 102.341 118.847 \n\n"))
#Read 180 items

Pop.3<- scan(textConnection("1855.98 1545.1 1286.75 1188.6 1143.84

1135.02 1159.33 1171.61 1180.07 1188.4 1197.23 1206.61 1215.38 1223.8  
1232.34 1241.01 1249.76 1258.54 1267.36 1276.25 1285.21 1294.23  
1303.31 1312.45 1321.66 1331.67 1341.37 1351.11 1360.94 1370.89 \n\n"))
#Read 30 items
closeAllConnections()
-- 
David.

>
> ______________________________________________
> 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.
David Winsemius, MD West Hartford, CT ______________________________________________ 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.
Received on Mon 31 May 2010 - 14:10:22 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 31 May 2010 - 14:20:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive