I want to simply write a space-separated list of integers to a file in R. I can read a space-separated list from a file using scan
, but is there function for doing the reverse? In other words, how can I write a vector of integers to a file in such a way that I can scan
the file later to read the same vector back in?
I don't want anything fancy like save
or write.table
.
scan() function in R Language is used to scan and read data. It is usually used to read data into vector or list or from file in R Language.
MS R Open: how to stop scan() from keyboard input The only way I have found to stop is to use Esc, which cancels the entry.
At the 1: prompt just type Ctrl+V (the accelerator keys for Paste) and the numbers will be scanned into the named variable (x in this example). You can then paste in another set of numbers, or press Return to complete data entry.
I think what you want is to supply a file agrument to cat()
, which writes to the file without any extras.
> cat(1:20,file="foobar.txt")
> x <- scan("foobar.txt")
Read 20 items
> x [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With