1

I want to capture data values from a post on SE into RStudio, and I manage to do so by copying the values, and then pasting them into the following command in the console:

> a = as.numeric(read.table(text = "8 8 4 1 2 2 0 2 5 2 3 3 3 1 5 4 4 1 4 2", sep = " "))
> a
 [1] 8 8 4 1 2 2 0 2 5 2 3 3 3 1 5 4 4 1 4 2

Now a is in the global environment. The problem is that I would like to save it into an R file containing a number of other things, let's call it file.R, where vector a would appear as:

a <- c(8, 8, 4, 1, 2, 2, 0, 2, 5, 2, 3, 3, 3, 1, 5, 4, 4, 1, 4, 2)

Unfortunately for me, the only way I know is to type the commas manually. How can I do this otherwise?

Antoni Parellada
  • 4,253
  • 6
  • 49
  • 114
  • 2
    `dput(a)`? http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Hugh Nov 23 '15 at 05:23
  • 2
    Check out `sodput` from [the "overflow" package](https://github.com/mrdwab/overflow-mrdwab). Usage would be `sodput(a)`, which is like `dput(a)` but it would include the `a <-` part. – A5C1D2H2I1M1N2O1R2T1 Nov 23 '15 at 06:05
  • @Hugh To be clear after I render the vector in the format I wanted in the console, if I want to then have it as such in the .R file, I still have to copy and paste it, correct? – Antoni Parellada Nov 23 '15 at 15:25
  • 1
    Yes although you can do something like 'write(dput(a), "file.R")' – Hugh Nov 23 '15 at 20:17

0 Answers0