IniziaInizia gratis

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".

Questo esercizio fa parte del corso

Linear Algebra for Data Science in R

Visualizza il corso

Istruzioni dell'esercizio

  • 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.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

# 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, ___)
Modifica ed esegui il codice