Skip to content

Instantly share code, notes, and snippets.

@viniciusmss
Created October 29, 2018 08:19
Show Gist options
  • Save viniciusmss/25d45e9ba902b75876cd56ab4480d72f to your computer and use it in GitHub Desktop.
Save viniciusmss/25d45e9ba902b75876cd56ab4480d72f to your computer and use it in GitHub Desktop.
# An addition
5 + 5
# A subtraction
5 - 5
# A multiplication
3 * 5
# A division
(5 + 5) / 2
# Exponentiation
2^5
# Modulo
28 %% 6
# Assign the value 42 to x
x <- 42
# Print out the value of the variable x
x
# Assign a value to the variables my_apples and my_oranges
my_apples <- 5
my_oranges <- 6
# Add these two variables together
my_apples + my_oranges
# Create the variable my_fruit
my_fruit = my_apples + my_oranges
# Change my_numeric to be 42
my_numeric <- 42
# Change my_character to be "universe"
my_character <- "universe"
# Change my_logical to be FALSE
my_logical <- FALSE
# Check class of my_numeric
class(my_numeric)
# Check class of my_character
class(my_character)
# Check class of my_logical
class(my_logical)
# Vectors
numeric_vector <- c(1, 10, 49)
character_vector <- c("a", "b", "c")
# Complete the code for boolean_vector
boolean_vector <- c(TRUE, FALSE, TRUE)
# 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")
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
# 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
# Check if you realized higher total gains in poker than in roulette
total_poker > total_roulette
# 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)]
# Define a new variable based on a selection
roulette_selection_vector <- roulette_vector[2:5]
# 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)
# Which days did you make money on poker?
selection_vector <- poker_vector > 0
# Print out selection_vector
selection_vector
# Select from poker_vector these days
poker_winning_days <- poker_vector[selection_vector]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment