[R] read in data file into R

Peter Ehlers ehlers at ucalgary.ca
Mon May 31 15:58:53 CEST 2010


On 2010-05-31 7:47, 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?
> 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.

Use scan().

>
> 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?

I would go the two-files route.

   -Peter Ehlers

>
> 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
>



More information about the R-help mailing list