Home / Programs / Create a vector using R Question 2
Programming Example

Create a vector using R Question 2

👁 396 Views
💻 Practical Program
📘 Step by Step Learning

After one week in Las Vegas and still zero Ferraris in your garage, you decide that it is time to start using your data analytical superpowers.

Before doing a first analysis, you decide to first collect all the winnings and losses for the last week:

For poker_vector:

  • On Monday you won $140
  • Tuesday you lost $50
  • Wednesday you won $20
  • Thursday you lost $120
  • Friday you won $240

For roulette_vector:

  • On Monday you lost $24
  • Tuesday you lost $50
  • Wednesday you won $100
  • Thursday you lost $350
  • Friday you won $10

You only played poker and roulette, since there was a delegation of mediums that occupied the craps tables. To be able to use this data in R, you decide to create the variables poker_vector and roulette_vector.

Program Code

# 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)

poker_vector
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)
> 
> poker_vector
[1]  140  -50   20 -120  240
> roulette_vector
[1]  -24  -50  100 -350   10

Explanation

Assign the winnings/losses for roulette to the variable roulette_vector.

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.