Home / Programs / Create a vector using R | Question 1
Programming Example

Create a vector using R | Question 1

👁 422 Views
💻 Practical Program
📘 Step by Step Learning

Let us focus first!

On your way from rags to riches, you will make extensive use of vectors. Vectors are one-dimension arrays that can hold numeric data, character data, or logical data. In other words, a vector is a simple tool to store data. For example, you can store your daily gains and losses in the casinos.

In R, you create a vector with the combine function C(). You place the vector elements separated by a comma between the parentheses. For example:

numeric_vector <- c(1, 2, 3)
character_vector <- c("a", "b", "c")

Once you have created these vectors in R, you can use them to do calculations.

Complete the code such that boolean_vector contains the three elements: TRUEFALSE and TRUE (in that order).

Program Code

numeric_vector <- c(1, 10, 49)
character_vector <- c("a", "b", "c")

# Complete the code for boolean_vector
boolean_vector <- c(TRUE, FALSE, TRUE)

Output

> numeric_vector
[1]  1 10 49
> character_vector
[1] "a" "b" "c"
> boolean_vector
[1]  TRUE FALSE  TRUE

Explanation

Vectors are one-dimension arrays that can hold numeric data, character data, or logical data. In other words, a vector is a simple tool to store data. For example, you can store your daily gains and losses in the casinos.

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.