Naming a vector in R | Question 2

R Programming Language / Vector in R Language

339

Program:

# Poker winnings from Monday to Friday
poker_vector <- c(140, -50, 20, -120, 240)

# Roulette winnings from Monday to Friday
roulette_vector <- c(-24, -50, 100, -350, 10)

# The variable days_vector
days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")

# Assign the names of the day to roulette_vector and poker_vector
names(poker_vector) <-  days_vector

# see poker_vector result
poker_vector

# see roulette_vector result
roulette_vector 

Output:

> # Poker winnings from Monday to Friday
> poker_vector <- c(140, -50, 20, -120, 240)
> 
> # Roulette winnings from Monday to Friday
> roulette_vector <- c(-24, -50, 100, -350, 10)
> 
> # The variable days_vector
> days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
> 
> # Assign the names of the day to roulette_vector and poker_vector
> names(poker_vector) <-  days_vector
> 
> # see poker_vector result
> poker_vector
   Monday   Tuesday Wednesday  Thursday    Friday 
      140       -50        20      -120       240 
> 
> # see roulette_vector result
> roulette_vector 
[1]  -24  -50  100 -350   10

Explanation:

  • A variable days_vector that contains the days of the week has already been created for you.
  • Use days_vector to set the names of poker_vector and roulette_vector.

This Particular section is dedicated to Programs only. If you want learn more about R Programming Language. Then you can visit below links to get more depth on this subject.