Introduction to Vectors answer

来源:互联网 发布:蒙古作文软件 编辑:程序博客网 时间:2024/06/06 01:51

Create a vector

# Define the variable vegasvegas <- "Go!"

Create a vector (2)

numeric_vector <- c(1, 10, 49)character_vector <- c("a", "b", "c")# Complete the code for boolean_vectorboolean_vector <-c(T,F,T)

Create a vector (3)

# Poker winnings from Monday to Fridaypoker_vector <- c(140, -50, 20, -120, 240)# Roulette winnings from Monday to Fridayroulette_vector <-c(-24,-50,100,-350,10)

Naming a vector

# Poker winnings from Monday to Fridaypoker_vector <- c(140, -50, 20, -120, 240)# Roulette winnings from Monday to Fridayroulette_vector <- c(-24, -50, 100, -350, 10)# Assign days as names of poker_vectornames(poker_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")# Assign days as names of roulette_vectorsnames(roulette_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")

Naming a vector (2)

# Poker winnings from Monday to Fridaypoker_vector <- c(140, -50, 20, -120, 240)# Roulette winnings from Monday to Fridayroulette_vector <- c(-24, -50, 100, -350, 10)# The variable days_vectordays_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")# Assign the names of the day to roulette_vector and poker_vectornames(poker_vector) <-days_vector   names(roulette_vector) <-days_vector

Calculating total winnings

A_vector <- c(1, 2, 3)B_vector <- c(4, 5, 6)# Take the sum of A_vector and B_vectortotal_vector <-A_vector+B_vector# Print out total_vectortotal_vector

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_vectornames(roulette_vector) <- days_vector# Assign to total_daily how much you won/lost on each daytotal_daily <-poker_vector+roulette_vector

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_vectornames(roulette_vector) <- days_vector# Total winnings with pokertotal_poker <- sum(poker_vector)# Total winnings with roulettetotal_roulette <-sum(roulette_vector)  # Total winnings overalltotal_week <-total_poker+total_roulette # Print out total_weektotal_week

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_vectornames(roulette_vector) <- days_vector# Calculate total gains for poker and roulettetotal_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

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_vectornames(roulette_vector) <- days_vector# Define a new variable based on a selectionpoker_wednesday <-poker_vector[3]

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_vectornames(roulette_vector) <- days_vector# Define a new variable based on a selectionpoker_midweek <-poker_vector[2:4] 

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_vectornames(roulette_vector) <- days_vector# Define a new variable based on a selectionroulette_selection_vector <-roulette_vector[2:5] 

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_vectornames(roulette_vector) <- days_vector# Select poker results for Monday, Tuesday and Wednesdaypoker_start <-poker_vector[c("Monday","Tuesday","Wednesday")] # Calculate the average of the elements in poker_startmean(poker_start)

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_vectornames(roulette_vector) <- days_vector# Which days did you make money on poker?selection_vector <-poker_vector>0# Print out selection_vectorselection_vector

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_vectornames(roulette_vector) <- days_vector# Which days did you make money on poker?selection_vector <- poker_vector > 0# Select from poker_vector these dayspoker_winning_days <-poker_vector[selection_vector] 

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_vectornames(roulette_vector) <- days_vector# Which days did you make money on roulette?selection_vector <-roulette_vector>0# Select from roulette_vector these daysroulette_winning_days <- roulette_vector[selection_vector]