1. Objects that contain a series of integers or words


You may use objects to store single values (integers, letters, words… see vectors) but you can store in fact much more than that. Using the function concatenate, you will be able to store a series of value or words under the same variable. Here is an example where we store a series of numbers in an object called series:

[code language=”r”]
series <- c(1,4,7,9,7,43,2,77,43,2,11,67,88,32,211,67,7654,6,3,9)
series

series[5]
series[1:5]
[/code]

concatenate numbers

As for vectors, the order in which the values are stored and displayed in the objects is stable. In other words, as exposed in the example above, the fifth value will always be 7, whether you check it now or tomorrow. It won’t be sorted automatically by the software while you are not watching… This means that specific data elements can be easily retrieved by typing the name of the object followed by the position of the data element of interest between brackets: series[5] (see above). If you want to retrieve all the values placed between the first and the fifth position, simply type series[1:5] to display the first 5 values.

Note that you can use concatenate within a command to retrieve specific values at specific positions. If you wish to retrieve the 10th, 20th and 30th values in the data set, follow this example:

[code language=”r”]
series[c(10,20,30)]
[/code]

retrieve NA

This command brings up the values 2 and 9 as the 10th and 20th values of the object series, but cannot find anything at the 30th position of the data set. In such a case, R tells you that nothing is to be displayed for that request and prints NA (Not Available) in the result line.

Using the same principles, you may build a object that contains text or strings. Here, for example, we create the vector norwegian.names that contains a series of 10 popular Norwegian names:

[code language=”r”]
norwegian.names <- c("Jan", "Per", "Bjørn", "Ole", "Kjell", "Lars", "Arne", "Knut", "Svein", "Hans")
norwegian.names

norwegian.names[5]
[/code]

 

norwegian names

As you see above, the object norwegian.names has been successfully created and can be displayed by typing its name. Also, it is possible to retrieve the fifth entry in the vector by adding brackets and the number 5 to the name of the vector, just as it was done with the vectors series in the previous example.

  Fant du det du lette etter? Did you find this helpful?
[Average: 0]