1.2 The assign operator and inputting a data vector into R


The 'assign operator' in R is used to assign a name to an object. For example, suppose we have a sample of 5 infants with ages (in months) of 6, 10, 12, 7, 15. In R, these values can be represented as a column vector (as a data set, these values would be arranged in one column for the variable age, with 5 rows). To enter these data into R and give the name 'agemos' to these data, we can use the command:

> agemos <- c(6,10,12,7,14)

The '>' is the ready prompt given by R, indicating that R is ready for our input (R typed the >, I typed the rest of the line). Here, agemos is the name we are giving to the object that we will be creating. The '<-' is the assign operator, and the 'c( …)' is a function creating a column vector from the indicated values. So we are creating the object 'agemos' which is a data vector (or variable in a data set).

To print an object, just enter the object name:

> agemos

[1] 6 10 12 7 14

The '[1]' the R gives at the start of the line is a counter – this line starts with the first value in the object (this is helpful with larger data sets when the print out extends over several lines). We can use this object name in later analyses. For example, the mean age of these 5 infants can be calculated using the 'mean( )' function:

> mean(agemos)

[1] 9.8

In R, object names are arbitrary and will generally vary to fit a particular application or study. Functions always involve parentheses to enclose the relevant arguments, and function names make up the R language. So, we might calculate mean age using mean(agemos) or mean cholesterol using mean(cholesterol); the function name is constant, but the object name varies to fit the particular study.

A copy of the R screen for the above analysis, with the input lines that we typed given in red and the output lines that R provides given in blue: