計算總獲利(3)
根據先前的分析,你有好日子也有壞日子。這和你的自信有點不符,所以你在想,會不會有一點點可能,整週加總下來其實是賠錢的?
可以幫你回答這個問題的函式是 sum()。它會計算向量中所有元素的總和。舉例來說,若要計算你玩撲克時整體輸贏的總金額,可以這樣做:
total_poker <- sum(poker_vector)
本練習屬於課程
R 入門
練習說明
- 計算你在輪盤的總輸贏金額,指派給變數
total_roulette。 - 現在你已經有輪盤和撲克的總額,可以輕鬆計算
total_week(也就是本週所有獲利與虧損的總和)。 - 列印
total_week。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# 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
# Total winnings with poker
total_poker <- sum(poker_vector)
# Total winnings with roulette
total_roulette <-
# Total winnings overall
total_week <-
# Print out total_week