R Tutorials
R Data Structures
R Graphics
R Statistics
R Examples
A vector is simply a list of objects of the same type.
To compile a vector list, use function c()
and comma-separate items.
In the example below, we create a variable vector called fruits, which includes strings:
In this example, we create a vector that includes numerical values:
To create a vector with numerical values respectively, use: operator:
You can also create numeric values by decimal, respectively, but be aware that if the last item is not in sequence, it is not used:
Result
[1] 1.5 2.5 3.5 4.5 5.5 6.5
[1] 1.5 2.5 3.5 4.5 5.5
In the example below, we create a logical value vector:
To find out how many vector objects, use the length()
function:
To sort objects in vector alphabetically or by numbers, use the sort()
function:
You can access vector objects by referring to its reference number inside brackets []
. The first item has point 1, the second item has point 2, and so on:
You can also access multiple elements by referring to different reference positions with function c()
:
You can also use negative reference numbers to access all items other than those specified:
To change the value of an item, see the reference number:
To repeat vectors, use rep()
function:
Repeat each value:
In one of the examples above, we have shown you how to create a vector with numerical values in sequence: operator:
To perform larger or smaller steps in sequence, use the seq function ():
Note: The function of seq()
has three parameters: from
where the sequence begins, to
where the sequence stops, and by
it is the sequence period.