Course Content
R Introduction: Part I
1. Basic Syntax and Operations
R Introduction: Part I
Vectors
It's time to delve into how to store numerous values within a single variable, which can be efficiently done using vectors.
In the introductory chapter of this section, we discussed that a vector acts as a local repository for various values. It's essential to grasp that a vector must contain data of the same type; this means you cannot mix integers, complex numbers, and logical values in one vector.
Creating a vector in R is straightforward: just list all the values separated by commas inside the c()
function. Here's how you do it:
When you input different data types within the c()
function, R will default to one type for all elements. For instance, in a vector like c(1, 2, 'some text')
, all numeric values will be converted to text. The reasoning is straightforward: if you attempt to construct a vector with varying types, R will select the one type to which all values can be coerced.
Task
Now, let's examine which types will be adopted when a vector consists of different data types.
- Display the vector containing the elements
0
,10.5
, and20
, in that specific order. - Display the vector containing the elements
1
,2.5
, and5 + 10i
, in that order. - Display the vector containing the elements
2.5
,TRUE
, and5
, in that order. - Display the vector containing the elements
FALSE
,25.5
, and'R'
, in that order.
Everything was clear?