๐ 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.
