# Intro
# This video is a quick and easy overview of the R programming language.
# We'll look at setting up our environment and go through the basic commands.
# - R is a programming language and software environment for statistical computing and graphics.
# - It is free and open source. It first appeared in 1993 and has gone through number of releases.
# - Today R is widely used for data analysis among statisticians and data scientists.
# RStudio
# RStudio is a free environment for R scripting. You can download RStudio at https://www.rstudio.com/.
# RStudio workspace consists of 4 panes:
# 1. Source pane for viewing and writing R scripts
# 2. Console pane for live coding
# 3. Environment/History pane for exploring active variables or viewing older commands
# 4. Files/Plots/Packages/Help pane for viewing plots and help and exploring files or packages
#Arithmetics
x=10
y=2
x+y
x-y
x*2
x/y
x^y #power
x**y #power
x%%y #remainder from division
# Relational operators
x=10
y=2
xy
x<=y
x>=y
x==y
x!=y
# Math functions
abs(x) #Takes the absolute value of x
log(x,base=y) #Takes the logarithm of x with base y; if base is not specified, returns the natural logarithm
exp(x) # Returns the exponential of x
sqrt(x) # Returns the square root of x
# Help on functions
help(log)
# Datasets and basic commands
iris
# We usually work with objects of class data.frame, a table look-alike with columns and rows
class(iris)
# Dimension
dim(iris)
# Column names
names(iris)
# First 6 rows of dataframe
head(iris)
# Specifying number of first rows
head(iris, 10)
# Last 6 rows of dataframe
tail(iris)
# Specifying number of last rows
tail(iris, 3)
# Descriptive statistics
summary(iris)
min(iris$Sepal.Length)
max(iris$Sepal.Length)
range(iris$Sepal.Length)
mean(iris$Sepal.Length)
median(iris$Sepal.Length)
mode(iris$Sepal.Length)
sd(iris$Sepal.Length)
quantile(iris$Sepal.Length)
# Point plot
library(ggplot2)
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point()
# Add colors to groups
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species)) + geom_point()
# Histogram
ggplot(iris, aes(x = Sepal.Length)) + geom_histogram()
# Add colors to groups
ggplot(iris, aes(x = Sepal.Length, fill = Species)) + geom_histogram()
# Boxplot
ggplot(iris, aes(x = Species, y = Sepal.Length)) + geom_boxplot()
# Saving images
ggsave(filename = "my_ggplot.png", path = "C:/temp")