CommencerCommencer gratuitement

Creating Vectors in R

Vectors can be analyzed in a few different ways in R.

One way is to create the vector yourself, and there are a few different means by which you can do this.

The rep() command in R creates a vector that repeats the same element a prescribed number of times, while the seq() command creates a vector that follows a prescribed pattern.

A vector can be manually created by using the c() command, which stands for "concatenate".

Cet exercice fait partie du cours

Linear Algebra for Data Science in R

Afficher le cours

Instructions

  • Use the example creating a vector of three 3's to create a vector of four 4's using the rep() command.

  • Use the example creating the vector containing the numbers 2, 4, and 6 in order, to create the vector containing 1, 3, and 5 in order using the seq() command.

  • The prompts to the first two questions have been created using the c() command. Provide the answers to the first two the same way, using the c() command.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# Creating three 3's and four 4's, respectively
rep(3, 3)
rep(4, ___)

# Creating a vector with the first three even numbers and the first three odd numbers
seq(2, 6, by = 2)
seq(1, 5, by = ___)

# Re-creating the previous four vectors using the 'c' command
c(3, 3, 3)
c(___, 4, ___, 4)

c(2, 4, 6)
c(___, 3, ___)
Modifier et exécuter le code