"DC Introduction to R - Vectors"의 두 판 사이의 차이

 
(같은 사용자의 중간 판 2개는 보이지 않습니다)
1번째 줄: 1번째 줄:
[[분류: DC Introduction to R]]
[[분류: DC Introduction to R]]
==개요==
{| class='wikitable count'
{| class='wikitable count'
! # !! [[DC Introduction to R]]
! # !! [[DC Introduction to R]]
20번째 줄: 19번째 줄:
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=1
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=1
<source lang='r'>
<source lang='r'>
# Define the variable vegas
vegas <- "Go!"
</source>
</source>


25번째 줄: 26번째 줄:
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=2
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=2
<source lang='r'>
<source lang='r'>
numeric_vector <- c(1, 10, 49)
character_vector <- c("a", "b", "c")
# Complete the code for boolean_vector
boolean_vector <- c(TRUE, FALSE, TRUE)
</source>
</source>


30번째 줄: 36번째 줄:
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=3
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=3
<source lang='r'>
<source lang='r'>
# 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)
</source>
</source>


35번째 줄: 46번째 줄:
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=4
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=4
<source lang='r'>
<source lang='r'>
# 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")
</source>
</source>


40번째 줄: 62번째 줄:
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=5
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=5
<source lang='r'>
<source lang='r'>
# 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
</source>
</source>


45번째 줄: 79번째 줄:
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=6
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=6
<source lang='r'>
<source lang='r'>
A_vector <- c(1, 2, 3)
B_vector <- c(4, 5, 6)
# Take the sum of A_vector and B_vector
total_vector <- A_vector + B_vector
 
# Print out total_vector
print(total_vector)
#[1] 5 7 9
</source>
</source>


50번째 줄: 93번째 줄:
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=7
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=7
<source lang='r'>
<source lang='r'>
# 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
# Assign to total_daily how much you won/lost on each day
total_daily <- poker_vector + roulette_vector
</source>
</source>


55번째 줄: 107번째 줄:
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=8
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=8
<source lang='r'>
<source lang='r'>
# 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 <- sum(roulette_vector)
# Total winnings overall
total_week <- total_poker + total_roulette
# Print out total_week
print( total_week )
#[1] -84
</source>
</source>


60번째 줄: 131번째 줄:
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=9
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=9
<source lang='r'>
<source lang='r'>
# 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
total_poker <- sum(poker_vector)
total_roulette <- sum(roulette_vector)
# Check if you realized higher total gains in poker than in roulette
total_poker > total_roulette
#[1] TRUE
</source>
</source>


65번째 줄: 150번째 줄:
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=10
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=10
<source lang='r'>
<source lang='r'>
# 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]
</source>
</source>


70번째 줄: 164번째 줄:
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=11
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=11
<source lang='r'>
<source lang='r'>
# 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_midweek <- poker_vector[c(2, 3, 4)]
</source>
</source>


75번째 줄: 178번째 줄:
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=12
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=12
<source lang='r'>
<source lang='r'>
# 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
roulette_selection_vector <- roulette_vector[2:5]
</source>
</source>


80번째 줄: 192번째 줄:
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=13
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=13
<source lang='r'>
<source lang='r'>
# 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
# Select poker results for Monday, Tuesday and Wednesday
poker_start <- poker_vector[c("Monday","Tuesday","Wednesday")]
 
# Calculate the average of the elements in poker_start
mean(poker_start)
#[1] 36.66667
</source>
</source>


85번째 줄: 210번째 줄:
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=14
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=14
<source lang='r'>
<source lang='r'>
# 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
# Which days did you make money on poker?
selection_vector <- poker_vector > 0
# Print out selection_vector
selection_vector
#  Monday  Tuesday Wednesday  Thursday    Friday
#    TRUE    FALSE      TRUE    FALSE      TRUE
</source>
</source>


90번째 줄: 229번째 줄:
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=15
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=15
<source lang='r'>
<source lang='r'>
# 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
# Which days did you make money on poker?
selection_vector <- poker_vector > 0
# Select from poker_vector these days
poker_winning_days <- poker_vector[selection_vector]
</source>
</source>


95번째 줄: 246번째 줄:
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=16
* https://campus.datacamp.com/courses/free-introduction-to-r/chapter-2-vectors-2?ex=16
<source lang='r'>
<source lang='r'>
# 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
# Which days did you make money on roulette?
selection_vector <- roulette_vector > 0
# Select from roulette_vector these days
roulette_winning_days <- roulette_vector[selection_vector]
</source>
</source>
==같이 보기==
* [[R 벡터]]

2019년 4월 6일 (토) 02:06 기준 최신판

# DC Introduction to R
DC Introduction to R - Intro to basics
DC Introduction to R - Vectors
DC Introduction to R - Matrices
DC Introduction to R - Factors
DC Introduction to R - Data frames
DC Introduction to R - Lists

1 Create a vector[ | ]

# Define the variable vegas
vegas <- "Go!"

2 Create a vector (2)[ | ]

numeric_vector <- c(1, 10, 49)
character_vector <- c("a", "b", "c")

# Complete the code for boolean_vector
boolean_vector <- c(TRUE, FALSE, TRUE)

3 Create a vector (3)[ | ]

# 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)

4 Naming a vector[ | ]

# 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")

5 Naming a vector (2)[ | ]

# 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

6 Calculating total winnings[ | ]

A_vector <- c(1, 2, 3)
B_vector <- c(4, 5, 6)

# Take the sum of A_vector and B_vector
total_vector <- A_vector + B_vector
  
# Print out total_vector
print(total_vector)
#[1] 5 7 9

7 Calculating total winnings (2)[ | ]

# 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

# Assign to total_daily how much you won/lost on each day
total_daily <- poker_vector + roulette_vector

8 Calculating total winnings (3)[ | ]

# 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 <- sum(roulette_vector) 

# Total winnings overall
total_week <- total_poker + total_roulette

# Print out total_week
print( total_week )
#[1] -84

9 Comparing total winnings[ | ]

# 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
total_poker <- sum(poker_vector)
total_roulette <- sum(roulette_vector)

# Check if you realized higher total gains in poker than in roulette 
total_poker > total_roulette
#[1] TRUE

10 Vector selection: the good times[ | ]

# 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]

11 Vector selection: the good times (2)[ | ]

# 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_midweek <- poker_vector[c(2, 3, 4)]

12 Vector selection: the good times (3)[ | ]

# 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
roulette_selection_vector <- roulette_vector[2:5]

13 Vector selection: the good times (4)[ | ]

# 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

# Select poker results for Monday, Tuesday and Wednesday
poker_start <- poker_vector[c("Monday","Tuesday","Wednesday")]
  
# Calculate the average of the elements in poker_start
mean(poker_start)
#[1] 36.66667

14 Selection by comparison - Step 1[ | ]

# 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

# Which days did you make money on poker?
selection_vector <- poker_vector > 0

# Print out selection_vector
selection_vector
#   Monday   Tuesday Wednesday  Thursday    Friday 
#     TRUE     FALSE      TRUE     FALSE      TRUE

15 Selection by comparison - Step 2[ | ]

# 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

# Which days did you make money on poker?
selection_vector <- poker_vector > 0

# Select from poker_vector these days
poker_winning_days <- poker_vector[selection_vector]

16 Advanced selection[ | ]

# 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

# Which days did you make money on roulette?
selection_vector <- roulette_vector > 0

# Select from roulette_vector these days
roulette_winning_days <- roulette_vector[selection_vector]

17 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}