πŸ“Š R 기초 | 벑터 λ§Œλ“€κΈ°, 넀이밍, 벑터 μ—°μ‚°, νŠΉμ • μ›μ†Œ μ„ νƒν•˜κΈ°

2020. 9. 12. 11:13ㆍLearning archive/Data Science

πŸ“ŒR의 데이터 ꡬ쑰 

 

Image credit @Gaurav Tiwari (Medium) 

 

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.

 

Image credit datacamp.com