R tip: read data into R from code file

Most often, we enter data from an external data file. Sometime when we have a very small dataset like from a tutorial or the r-help mailing list, it is more concise to include it as part of the R code. We can use read.table() but use text=”” instead of file=””.

I ran into this from Rui Barradas on the r-help mailing list at https://stat.ethz.ch/pipermail/r-help//2013-May/353030.html.

dat <- read.table(text = "
X     Y
0    137.659166666667
2.502    152.623333333333
4.998    162.391333333333
7.501    170.753666666667
9.997    174.877
12.499    178.4275
15.002    184.565
17.498    189.8985
20    192.6885
", header = TRUE)
dat

Output:

       X        Y
1  0.000 137.6592
2  2.502 152.6233
3  4.998 162.3913
4  7.501 170.7537
5  9.997 174.8770
6 12.499 178.4275
7 15.002 184.5650
8 17.498 189.8985
9 20.000 192.6885