next up previous
Next: Arithmetic and Logical Operators Up: Introduction to S Previous: On-line Documentation

Data in S

The basic data structure in S is a list of values, called a data set or a variable. A variable may contain numbers, character strings, or logical values (`T' and `F'). Data sets may be combined into more complex data structures, such as arrays, lists, and `data frames'. To create a variable, just assign an expression to a name. To create sets of data, it may be useful to use the combine function, `c( )', a sequence operator of the form `n : m', or the repeat function, `rep( )'.

You can print the contents of a variable by typing its name:

> x <- c(2.3, 4.5, 9, 6.7, 8.23)
> x
[1] 2.30 4.50 9.00 6.70 8.23

> y <- 1:5
> y
[1] 1 2 3 4 5

> z <- rep(2,7)
> z
[1] 2 2 2 2 2 2 2

> colors <- c("red","green","blue")
> colors
[1] "red"   "green" "blue" 

> # comment: S ignores anything to the right of a sharp sign (#)
Note: the `arrow' indicating assignment is two keystrokes, a `less than' follwed by a `minus'.

The first assignment above creates a variable named `x' containing the listed numbers. The second assignment in the example above uses the sequence operator (`:'), ie. `n:m' is the sequence of integers starting with `n' and ending with `m'. `2:-2' is the same as `c(2, 1, 0, -1, -2)'. The last assignment in the example above uses the `repeat' function: repeat the value `2' seven times.

You may assign the result of any S expression to a variable. Names of variables should start with an alphabetic character, and may contain lower case or upper case letters, numbers, or periods. Names are case sensitive: x is not the same as X. Be careful not to use S function names such as `c', `var', or `t' for variable names. Do not use special characters in names!

NOTE: If you see a cryptic warning message about looking for an object of mode function and ignoring one of mode data, it probably means that you have used an S function name as a variable name. These warning messages do no harm, but are irritating. You can assign the offending variable to a new name, and then delete the old variable by using the `rm()' function.

To see a list of the datasets you have created, use the `objects()' function:

> objects()
> objects()
[1] "colors" "x"           "y"           "z"          

> rm(colors)  #remove a variable
> objects()
[1] "x" "y" "z"

Finally, you can enter data from a file, using the either the scan function, for simple datasets, or the read.table function for data in tabular format. In either case, the file should be in plain text format, with values separated by spaces or tabs.

> x <- scan("file_name")

Data Tables

There are several ways to enter a table of data in S. The most convenient is probably the read.table command.


next up previous
Next: Arithmetic and Logical Operators Up: Introduction to S Previous: On-line Documentation

Albyn Jones
Tue Jun 25 11:03:47 PDT 1996