Go to the first, previous, next, last section, table of contents.


Input and Output

What kinds of data can S read?

S comes with functions designed to read ASCII files. It also has the ability to invoke commands in the operating system and to interface with C and Fortran programs. These can be used to access data kept in other forms, in database management systems, etc.

How do I read data into S/S-PLUS from an ASCII file?

The scan() function can be used to read data from a text file or interactively from standard input. The function make.fields() can be used to create fields with a specified field separator so that the file can be used as input to scan().

The read.table() function reads an ASCII file and creates a data frame (Refer to the White book (See section What documentation is available for S, S-PLUS?, for more info) for information about data frames).

S-PLUS 4.0 for Windows allows data import and export from a variety of file formats such as Excel, SAS, and SPSS. See the File:Import Data:From File menu item or the import.data() and export.data() functions.

How can I write S data to an ASCII file?

The write() function allows you to write S data into a file in ASCII format.

The functions print(), format(), cat() and paste() can be used to format output to be written on to the files.

The sink() function allows you to enter output from S/S-PLUS commands into a file.

The data.dump(), dump() and dput() functions write S objects into ASCII files but not in regular text format. They are used for data transfer between machines.

For more info, See section Can S/S-PLUS objects be transferred from one machine to another?, and See section When is dump() and restore()/source() preferable to data.dump()/data.restore()?.

Can S/S-PLUS objects be transferred from one machine to another?

S objects are stored as binary files for efficiency when they are accessed. Because these files contain hardware-dependent information (floating point representations, for example), they should not be moved directly from one machine to another unless you are sure that the underlying machine arithmetic and storage policies are identical.

The portable way to move S objects is to convert them to an ASCII file using the data.dump() function; the file can be moved to the new machine and the objects recreated using data.restore().

For more information,See section When is dump() and restore()/source() preferable to data.dump()/data.restore()?.

When is dump() and restore()/source() preferable to data.dump()/data.restore()?

The only advantage of dump()/restore() over data.dump()/data.restore() is that the ASCII file produced is easy to read and change. Thus dump is often used to produce ASCII files of S functions which are then edited and redefined using source or restore. However, for any S data objects that are not small, data.dump is recommended since it is faster and uses much less memory.

When dump() and restore() were created they were intended to accommodate the entire range of S data structures using the same syntax as the S language did. restore() parses and then evaluates each dump'ed object. If you have a matrix containing 10000 numbers, restoration of the file executes the c() function with 10000 arguments. That takes quite a bit of space to parse and evaluate.

data.dump() and data.restore() are designed to be used in the same way as dump() and restore() but deal with an ASCII representation that can be efficiently turned back to S objects.

Why does round() sometimes not print rounded values?

There are two stages in rounding--the first step is producing an internal representation of the rounded value. For example,

> x <- .123450000001
> y <- round(x,3)

uses the machine's floating point arithmetic to produces the best approximation to the the numeric value .123 in y. That's all the round() function does.

The next step is printing this value or incorporating it into a text string. It is at this stage when things can go astray. The S print function, invoked automatically when S objects are printed, tries hard to produce a pretty visual representation of the value being printed.

> x
[1] 0.12345
> y
[1] 0.123

(options(digits=) controls how many digits the print function thinks are important).

Other functions that convert numeric to character, may not produce results as "pretty" as print does:

> as.character(x)
[1] "0.123450000001"
> as.character(y)  
[1] "0.123"

Depending on the machine's arithmetic, there may even be instances where as.character() (or cat() or paste()) will produce extra digits from a rounded value.

The solution is to use the function format(), to turn the numeric value into a "pretty" character value:

> format(x)
[1] "0.12345"

This is particularly important in building character strings

> paste("r = ",format(round(x, dig=4)))
[1] "r =  0.1234"


Go to the first, previous, next, last section, table of contents.