# Super Fast Crash Course in R

# Minimum commands to know to get started using R


# Assignment

# integer
i <- 23
i

variable <- 23
variable

# double
d <- 2.342324
d

# string
s <- "hello world"
s

# boolean
b <- FALSE
b

# crear un vector de iteraciones
id <- seq(0,100,0.5)

b <- id >50

# Data Structures


# Vector

# create a vector using the c() function
v <- c(98, 99, 100)
v
v[3]

# create a vector from a range of integers
r <- (1:10)
r
r[5:10]

# add a new item to the end of a vector
v <- c(1, 2, 3)
v[4] <- 4
v


# List

# create a list of named items
a <- list(aa=1, bb=2, cc=3)
a
a$bb

lista <- list(campo1=b, campo2=id, campo3=50)
lista
lista$campo2


# add a named item to a list
lista$campo4=4
lista$campo4


# Array

data <- c(1, 2, 3)
v <- array(data)
v


# Matrix

# Create a 2-row, 3-column matrix with named headings
data <- c(1, 2, 3, 4, 5, 6)
headings <- list(c("fila1","fila2"), c("a","b","c"))
m <- matrix(data, nrow=2, ncol=3, byrow=TRUE, dimnames=headings)
m
m[1,]
m[,1]


# Data Frame

# create a new data frame
years <- c(1980, 1985, 1990)
scores <- c(34, 44, 83)
type <- c("low","low","high")
df <- data.frame(years, scores,type)
df[,1]
AUX <- df$scores

# Convert a data frame to a matrix
m <- data.matrix(df)
m

# Convert the matrix to a data frame
df <- as.data.frame(m)
df


# Flow Control

# if then else
a <- 33
if (a > 55) {
	print("a is more than 55")
} else {
	print("A is less than or equal to 55")
}

# for loop
mylist <- 1:100
for (i in mylist) {
	print(i)
}

# while loop
a <- 100
while (a < 500) {
	a <- a + 100
}
a

# Functions

# call function to calculate the mean on a vector of integers
numbers <- c(1, 2, 3, 4, 5, 6)
mean(numbers)
max(numbers)
quantile(numbers,probs = c(0.1,0.6))
summary(numbers)

# help with the mean() function
?mean
help(mean)

# example usage of the mean function
example(mean)

# function arguments
args(mean)

# define custom function
mysum <- function(a, b, c) {
	sum <- a + b + c
	return(sum)
}
# call custom function
sumatotal <- mysum(1,2,3)



# Packages

# install the caret package
install.packages("caret")

# load the caret package
library(caret)

# help for the caret package
library(help="caret")



# Other

# R documentation
help.start()

# quit
q()


