R Programming
  • The wikipedia of R by me
  • Hello R
    • -What is R & RStudio
    • -Learning sources
    • -R online editor
    • -R environment
  • Data types
    • -Dealing with Number
    • -Dealing with String
    • -Dealing with Dates
    • -Dealing with NA's
    • -Dealing with Logicals
    • -Dealing with Factors
  • R data
    • -R object
    • -Data structures
      • --Basics
      • --Managing Vectors
      • --Managing Matrices
      • --Managing Data Frames
    • -Functions
    • -Importing/exporting data
    • -Shape&Transform data
    • -R management
  • Visualizations
  • Intro to R Bootcamp
    • -01-introduction
    • -02-data preparation
    • -03-data transformation
    • -04-visualization
  • R programming track
    • -a-Introduction to R
      • --1-Intro to basics
      • --2-Vectors
      • --3-Matrices
      • --4-Factors
      • --5-Data frames
      • --6-Lists
    • -b-Intermediate R
      • --1-Conditionals and Control Flow
      • --2-Loops
      • --3-Functions
      • --4-The apply family
      • --5-Utilities
    • -d-Writing Functions in R
      • --1-A quick refresher
      • --2-When and how you should write a function
      • --3-Functional programming
      • --4-Advanced inputs and outputs
      • --5-Robust functions
  • Data Wrangling with R
  • R-tutor
    • #R introduction
    • #Elementary Statistics with R
  • Hands-On Programming with R
  • R for Data Science
  • Advanced R
  • ggplot2
  • R packages
  • Statistik-1
  • Statistik-2
  • Statistik-3
  • Zeitreihen & Prognosen
  • Descriptive Analytics
  • Predictive Analytics
  • Prescriptive Analytics
  • R Graphics Cookbook
    • ggplot2 intro
    • ggplot2 custome
    • ggplot top-50
  • #Exploratory Data Analysis
    • -Data Summary
    • -Checklist Solution
  • #Data Mining
    • Untitled
    • Untitled
  • #Machine Learning
    • Intro to ML
    • Intro alghorithms
    • 1. Supervised Learning
  • Master R for Data Science
    • Learning R
    • Untitled
    • Untitled
  • Data Science Projects
    • Simple linear regression:
Powered by GitBook
On this page
  1. R programming track
  2. -a-Introduction to R

--2-Vectors

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

#Assign the winnings/losses for roulette to the variable roulette_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)

#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_vectors
names(roulette_vector) <- c("Monday", "Tuesday", 
"Wednesday", "Thursday", "Friday")

#5-Naming a vector (2)

#Use days_vector to set the names of poker_vector and roulette_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)
  
# 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

#Take the sum of the variables A_vector and B_vector and 
# it assign to total_vector.
#Inspect the result by printing out total_vector.    
    
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
total_vector

#7-Calculating total winnings (2)

#Assign to the variable total_daily how much you won or lost 
#on each day in total (poker and roulette combined). 
​
# 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 <- roulette_vector+ poker_vector
total_daily

#8-Calculating total winnings (3)

#Calculate the total amount of money that you have won/lost-
#with roulette and assign to the variable total_roulette.
#Now that you have the totals for roulette and poker,- 
#you can easily calculate total_week-
#(which is the sum of all gains and losses of the week).
#Print out total_week.  
​
# 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_roulette+total_poker
  
# Print out total_week
  total_week

#9-Comparing total winnings

#Calculate total_poker and total_roulette as in the previous exercise. 
#Use the sum() function twice.
#Check if total gains in poker are higher than for roulette by 
#(using a comparison. 
#Simply print out the result of this comparison. 
#What do you conclude, should you focus on roulette or on poker?
​
# 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_poker
  total_roulette <-sum(roulette_vector)
  total_roulette
  
# Check if you realized higher total gains in poker than in roulette 
  total_poker>total_roulette

#10-Vector selection: the good times

#Assign the poker results of Wednesday to the variable poker_wednesday.
# 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]
  poker_wednesday

#11-Vector selection: the good times (2)

#Assign the poker results of Tuesday, Wednesday and Thursday 
#to the variable poker_midweek.   
    
# 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)]
  poker_midweek

#12-Vector selection: the good times (3)

#Assign to roulette_selection_vector the roulette results 
#from Tuesday up to Friday; 
#make use of : if it makes things easier for you. 
   
# 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]
  roulette_selection_vector
  

#13-Vector selection: the good times (4)

#Select the first three elements in poker_vector by using their names:- 
#"Monday", "Tuesday" and "Wednesday". 
#Assign the result of the selection to poker_start.
#Calculate the average of the values in poker_start with 
#the mean() function. 
#Simply print out the result so you can inspect it.
​
# 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")]
  poker_start
  
# Calculate the average of the elements in poker_start
  mean(poker_start)

#14-Selection by comparison - Step 1

#Check which elements in poker_vector are positive (i.e. > 0)- 
#and assign this to selection_vector.
#Print out selection_vector so you can inspect it. 
#The printout tells you whether you won (TRUE) or lost (FALSE)-
#any money for each day.    
​
​
# 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

#15-Selection by comparison - Step 2

#Use selection_vector in square brackets to assign the amounts-
#that you won on the profitable days to the variable poker_winning_days.  
​
# 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
  selection_vector
  
# Select from poker_vector these days
  poker_winning_days <- poker_vector[selection_vector]
  poker_winning_days   

#16-Advanced selection

#Create the variable selection_vector, this time to see if you 
#made profit with- 
#roulette for different days.
#Assign the amounts that you made on the days that you ended 
#positively for- 
#roulette to the variable roulette_winning_days.
#This vector thus contains the positive winnings of roulette_vector.
​
# 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
  selection_vector
  
# Select from roulette_vector these days
  roulette_winning_days <- roulette_vector[selection_vector]
  roulette_winning_days  

Previous--1-Intro to basicsNext--3-Matrices

Last updated 6 years ago