Home / Programs / Calculating total winnings in R | Calculation using vector | Question 2
🚀 Programming Example

Calculating total winnings in R | Calculation using vector | Question 2

👁 406 Views
💻 Practical Program
📘 Step Learning

Read This Question To understand this Question, because the previous question is liked to this question.

Now you understand how R does arithmetic with vectors, it is time to get those Ferraris in your garage! First, you need to understand what the overall profit or loss per day of the week was. The total daily profit is the sum of the profit/loss you realized on poker per day, and the profit/loss you realized on roulette per day.

In R, this is just the sum of roulette_vector and poker_vector.

Instruction

Assign to the variable total_daily how much you won or lost on each day in total (poker and roulette combined).

💻 Program Code

# Poker and roulette winnings from Monday to Friday:
poker_vector <- c(140, -50, 20, -120, 240)
roulette_vector <- c(-24, -50, 100, -350, 10)
days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
names(poker_vector) <- days_vector
names(roulette_vector) <- days_vector

# Assign to total_daily how much you won/lost on each day
total_daily <-  poker_vector + roulette_vector

# See Result
total_daily
                        

🖥 Program Output

> # Poker and roulette winnings from Monday to Friday:
> poker_vector <- c(140, -50, 20, -120, 240)
> roulette_vector <- c(-24, -50, 100, -350, 10)
> days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
> names(poker_vector) <- days_vector
> names(roulette_vector) <- days_vector
> 
> # Assign to total_daily how much you won/lost on each day
> total_daily <-  poker_vector + roulette_vector
> 
> # See Result
> total_daily
   Monday   Tuesday Wednesday  Thursday    Friday 
      116      -100       120      -470       250 
                            

📘 Explanation

Calculation using vector
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.