Getting started

R is a programming language and software environment for statistical analysis, graphics representation and reporting.

This is a summary of the r language syntax: you can find more details in cran documentation.

Basics

[1]:
# initialization of a variable
my_numeric_variable <- 1
my_string_variable <- "hello"
# print a variable
my_numeric_variable # and type enter
print(my_string_variable)
1
[1] "hello"

Vectors

[2]:
# initialization of a vector
my_numeric_vector <- c(1,2,3,4,5,6)
my_sequence <- 1:25
my_string_vector <- c("hello", "world", "!")
my_logic_vector <- c(TRUE, FALSE, T, F)
assign("my_vector", c(1:6))
my_random_vector <- sample(1:5000, 25) # Vector with 25 elements with random number from 1 to 5000
[3]:
# operations with vectors
sum(my_numeric_vector)
mean(my_numeric_vector)
median(my_numeric_vector)
21
3.5
3.5
[4]:
# multiplication 2 with each element of a vector
my_vector*2
# division 2 with each element of a vector
my_vector/2
  1. 2
  2. 4
  3. 6
  4. 8
  5. 10
  6. 12
  1. 0.5
  2. 1
  3. 1.5
  4. 2
  5. 2.5
  6. 3
[5]:
# sum each element of a vector with another vector by position
my_new_vector <- my_numeric_vector + my_vector # allow
my_new_vector <- my_numeric_vector + my_sequence # allow but with warning
my_new_vector <- my_numeric_vector + c(1:5) # allow but with warning
Warning message in my_numeric_vector + my_sequence:
“longer object length is not a multiple of shorter object length”
Warning message in my_numeric_vector + c(1:5):
“longer object length is not a multiple of shorter object length”
[6]:
# get element from a vector
my_string_vector[1] # print hello
my_string_vector[-2] # print hello!
my_string_vector[1:2] # print hello world
my_string_vector[c(1,3)] # print hello!
'hello'
  1. 'hello'
  2. '!'
  1. 'hello'
  2. 'world'
  1. 'hello'
  2. '!'
[7]:
# add labels to a vector
names(my_vector) <- c("one","two","three","four","five","six")
print(my_vector)
  one   two three  four  five   six
    1     2     3     4     5     6
[8]:
# get data type of a vector
class(my_vector) # print integer
typeof(my_string_vector) # print character
mode(my_vector) # print numeric
'integer'
'character'
'numeric'
[9]:
# convertion of each element of a vector
as.character(my_vector)
  1. '1'
  2. '2'
  3. '3'
  4. '4'
  5. '5'
  6. '6'

Matrixes

A matrix is a vector with more dimensions

[10]:
# matrix
my_matrix <- matrix(1:10, nrow = 2, ncol = 5) # creates a matrix bidimensional with 2 rows and 5 columns
my_matrix
my_matrix[2,2] # prints 4
my_matrix[1,] # prints row 1
my_matrix[,2] # prints column 2
A matrix: 2 × 5 of type int
1357 9
246810
4
  1. 1
  2. 3
  3. 5
  4. 7
  5. 9
  1. 3
  2. 4
[11]:
# merge of vectors
vector_one <- c("one", 0.1)
vector_two <- c("two", 1)
vector_three <- c("three", 10)
my_vectors <- matrix(c(vector_one, vector_two, vector_three), nrow = 3, ncol = 2, byrow = 1)
colnames(my_vectors) <- c("vector number", "quantity")
rownames(my_vectors) <- c("yesterday", "today", "tomorrow")
[12]:
my_vectors
A matrix: 3 × 2 of type chr
vector numberquantity
yesterdayone 0.1
todaytwo 1
tomorrowthree10

Weighted average

[13]:
# performance
apple_performance <- 3
netflix_performance <- 7
amazon_performance <- 11
# weight
apple_weight <- .3
netflix_weight <- .4
amazon_weight <- .3
# portfolio performance
weighted_average <- apple_performance * apple_weight + netflix_performance * netflix_weight + amazon_performance * amazon_weight
weighted_average
7
[14]:
# the same sample but with vectors
performance <- c(3,7,11)
weight <- c(.3,.4,.3)
company <- c('apple','netflix','amazon')
names(performance) <- company
names(weight) <- company
performance_weight <- performance * weight
performance_weight
weighted_average <- sum(performance_weight)
weighted_average
apple
0.9
netflix
2.8
amazon
3.3
7

Functions

[15]:
# the weighted average but with a function
weighted_average_function <- function(performance, weight) {
    return(sum(performance * weight))
}
performance <- c(3,7,11)
weight <- c(.3,.4,.3)
weighted_average <- weighted_average_function(performance, weight)
weighted_average
7