π R κΈ°μ΄ | λ²‘ν° λ§λ€κΈ°, λ€μ΄λ°, λ²‘ν° μ°μ°, νΉμ μμ μ ννκΈ°
2020. 9. 12. 11:13γLearning archive/Data Science
πRμ λ°μ΄ν° ꡬ쑰
Rμμ μ 곡νλ λ°μ΄ν° ꡬ쑰λ 벑ν°, 맀νΈλ¦μ€, λ°°μ΄, λ°μ΄ν°νλ μ, 리μ€νΈκ° μλ€.
π λ²‘ν° λ§λ€κΈ°
#벑ν°λ, νλ νΉμ νλμ΄μμ μμλ₯Ό κ°μ§ μ μλ λ°μ΄ν° ꡬ쑰 ννμ΄λ€.
#νλμ 벑ν°λ λμΌν μλ£νμ
μ κ°μ ΈμΌ νλ€.
vector <- "yeah!"
#c(combine function)μ ν΅ν΄ μ¬λ¬ μμλ₯Ό κ°μ§ 벑ν°λ₯Ό μμ±ν μ μλ€.
numeric_vector <- c(1,50,60)
character_vector <- c("s", "a","n")
boolean_vector <-c(TRUE,FALSE,TRUE)
π λ²‘ν° μ΄λ¦ μ§μ νκΈ° names()
# 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)
# Assign days as names of poker_vector
names(poker_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
# Assign days as names of roulette_vector
names(roulette_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
π λ²‘ν° μ΄λ¦μ λ³μλ‘ μ μ₯ν λ€ μ§μ νκΈ°
# 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
names(roulette_vector) <- days_vector
π λ²‘ν° κ° μ°μ°
A_vector <- c(1, 2, 3)
B_vector <- c(4, 5, 6)
#μ λ 벑ν°λ₯Ό λνμ
total_vector <- A_vector + B_vector
# λν κ°μ μΆλ ₯νμ
total_vector
# console
[1] 5 7 9
# ν¬μ»€μ λ£°λ μμ΅ (μ-κΈ)
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 <- roulette_vector + poker_vector
total_daily
#console
> total_daily
Monday Tuesday Wednesday Thursday Friday
116 -100 120 -470 250
π λ 벑ν°μ μμμ ν©μ λΉκ΅ν΄λ³΄μ sum()
# 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
# Calculate total gains for poker and roulette
# sum()μΌλ‘ νλμ λ²‘ν° λ΄ μμλ₯Ό λͺ¨λ λνλ€
total_poker <-sum(poker_vector)
total_roulette <-sum(roulette_vector)
# μ΄λ€ 벑ν°κ° ν°μ§ νμΈν΄λ³΄μ!
total_poker > total_roulette
πλ²‘ν° νΉμ μμ μ ννκΈ° vector_name[n] | vector_name[n1:n2]
# 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
# Define a new variable based on a selection
poker_wednesday <-poker_vector[3]
#λ²‘ν° λ΄ μμ μ¬λ¬κ° μ ννκΈ°
# λ²‘ν° λ΄ μμ μ¬λ¬κ° μ ννκΈ°
# Define a new variable based on a selection
poker_midweek <- poker_vector[c(2,3,4)]
poker_midweek
#console
# Tuesday Wednesday Thursday
# -50 20 -120
#[c(2,3,4)]λ λ무 κΈΈλ€! [2:4]λ‘ μ νλ κ°λ₯νλ€ (2,3,4λ²μ§Έ μμλ₯Ό μ ννλ€)
# Define a new variable based on a selection
roulette_selection_vector <- roulette_vector[2:4]
roulette_selection_vector
#벑ν°μ μ§μ ν μ΄λ¦μΌλ‘ valueλ₯Ό μ νν μλ μλ€.
poker_start <- poker_vector[c("Monday","Tuesday","Wednesday")]
poker_start
#console
Monday Tuesday Wednesday
140 -50 20
π벑ν°μ νκ· κ° κ΅¬νκΈ° mean(vector_name)
mean(poker_start)
#console
[1] 36.66667
π selection by comparison | Advanced selection
# Which days did you make money on poker?
selection_vector <- poker_vector > 0
# Print out selection_vector
selection_vector
# console
Monday Tuesday Wednesday Thursday Friday
TRUE FALSE TRUE FALSE TRUE
# Select from poker_vector these days
poker_winning_days <- poker_vector[selection_vector]
poker_winning_days
# console
Monday Wednesday Friday
140 20 240
#R knows what to do when you pass a logical vector in square brackets:
#it will only select the elements that correspond to TRUE in selection_vector.