Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the reverse of scan() in R?

Tags:

file-io

r

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.

like image 585
Ryan C. Thompson Avatar asked Feb 19 '11 22:02

Ryan C. Thompson


People also ask

What is the use of scan () in R?

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.

How do you stop a scan in R?

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.

How do you scan in R studio?

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.


1 Answers

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
like image 55
Fojtasek Avatar answered Oct 12 '22 14:10

Fojtasek