벡터의 연산 Vector Arithmetic 의 기본은 간단하다.
기존의 덧셈, 뺄셈기호를 이용하여 계산할 수 있다. 아래에 다양한 연습문제를 통해 익혀보았다.
Summing and subtracting vectors
Take the element-wise sum of the variables A_vector and B_vector and it assign to total_vector. The result should be a vector. Inspect the result by printing total_vector to the console. Do the same thing, but this time subtract B_vector from A_vector and assign the result to diff_vector. Finally, print diff_vector to the console as well.
# A_vector and B_vector have already been defined for you
A_vector <- c(1, 2, 3)
B_vector <- c(4, 5, 6)
# Take the sum of A_vector and B_vector: total_vector
total_vector <- A_vector + B_vector
# Print total_vector
total_vector
# Calculate the difference between A_vector and B_vector: diff_vector
diff_vector <- A_vector - B_vector
# Print diff_vector
diff_vector
Calculate your earnings
Assign to the variable total_daily how much you won or lost on each day in total (poker and roulette combined).
# Casino 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
# Calculate your daily earnings: total_daily
total_daily <- poker_vector + roulette_vector
Calculate total winnings : sum()
Calculate the total amount of money that you have won/lost with poker and assign it to the variable total_poker. Do the same thing for roulette and assign the result to total_roulette. Now that you have the totals for roulette and poker, you can easily calculate total_week (which is the sum of all gains and losses of the week). Print the variable total_week.
# Casino 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
total_poker <- sum(poker_vector)
# Total winnings with roulette: total_roulette
total_roulette <- sum(roulette_vector)
# Total winnings overall: total_week
total_week <- total_poker + total_roulette
# Print total_week
total_week
Comparing total winnings
Create a new vector containing logicals, poker_better, that tells whether your poker gains exceeded your roulette results on a daily basis. Calculate total_poker and total_roulette as in the previous exercise. Using total_poker and total_roulette, Check if your total gains in poker are higher than for roulette by using a comparison. Assign the result of this comparison to the variable choose_poker and print it out. What do you conclude, should you focus on roulette or on poker?
# Casino 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
# Calculate poker_better
poker_better <- as.logical(poker_vector > roulette_vector)
# Calculate total_poker and total_roulette, as before
total_poker <- sum(poker_vector)
total_roulette <- sum(roulette_vector)
# Calculate choose_poker
choose_poker <- total_poker > total_roulette
# Print choose_poker
choose_poker
First steps in rational gambling
Use the sum() function twice in combination with the + operator to calculate the total gains for your entire past week in Vegas (this means for both poker and roulette). Assign the result to total_past. Calculate difference of past to present poker performance: Using the - operator, subtract poker_past from poker_present, to calculate diff_poker. diff_poker should be a vector with 5 elements.
# Calculate total gains for your entire past week: total_past
total_past <- sum(poker_past + roulette_past)
# Difference of past to present performance: diff_poker
diff_poker <- poker_present - poker_past
'etc.' 카테고리의 다른 글
LH사태 슬그머니 말바꾸기 시전중인 문정부 (0) | 2021.03.17 |
---|---|
사람들은 왜 '럼블(Rumble)'로 이동하는가 (0) | 2020.12.11 |
R 데이타 초보 / 벡터 이름 만들기 _ Introduction to R by EDX (0) | 2019.10.05 |
R 데이터 초보 / 벡터만들기 _ Introduction to R by EDX (0) | 2019.10.02 |
모든 것을 바꾸는 한 가지, 신뢰 - 줄거리 (0) | 2019.09.24 |
댓글