Currently Topical Topic: Random R code!

Ever had data given to you in text form (such as in a pdf file!) and hated having to type it out again? Never fear, for the following trivial R function can aid in reading such data into R by simple copy+paste of the text of data.


easydat = function(split = " +", perl = TRUE){
  vec = NULL
  inp = readline()
  while(inp != "END"){
    if(!is.null(split))
      inp = strsplit(inp, split, perl = perl)[[1]]
    vec = c(vec, inp)
    inp = readline()
  }
  vec
}

Example usage:

Call the function and store result:

> mydat = easydat()
Copy+Paste in your data, then finish with "END".

1 2 3  1 4 1 2 3 1  12 312 14 1 1
123 134 12 1 2 1 21 23 13 12 1
END
Check result. Note that the data is read as a character vector, if it is a numeric, you should change it (e.g. with as.numeric).

> mydat
 [1] "1"   "2"   "3"   "1"   "4"   "1"   "2"   "3"   "1"   "12"  "312" "14" 
[13] "1"   "1"   "123" "134" "12"  "1"   "2"   "1"   "21"  "23"  "13"  "12" 
[25] "1"

Data in forms such as the following can be used without changing any arguments:

Data with 1 or more spaces in between, possibly spread over multiple lines.

11 13 11  8  8  8  7  6 13 10  8  9 14 13 10 11  9  5 10  5  5 10  9  7  7
17 12 10  5 10 17 12  6 15 14 11  4 12  9 11  9 14  9 10  7  3  6  8  8  8
 8 13  5  9 12 14  4  4 10  9  8 10  6  7  9  9 10 13  7 11  4 10  7 11  9
11 10 19  6  8  6 15  8  9 12
Data spread over multiple lines.

11
13
11
8
8
8
7
6
13
10