R is an open source language and environment for statistical computing and graphics.
An implementation of the S language, which was developed at Bell Laboratories by Rick Becker, John Chambers and Allan Wilk.
It provides a wide variety of statistical and graphical techniques such as linear and nonlinear modelling, statistical tests, time series analysis, classification, and clustering.
RStudio; however, is a free, open source, user interface for R, that makes working with R much easier.
RStudio adds functionality to R, includes many menu options, and other ways to keep your work neat and organized.
R and RStudio are free, open source and available for Mac, Windows, and Linux operating systems. You should first install R before installing RStudio.
To download R, go to the R-project website and click on CRAN
, which is an acronym for Comprehensive R Archive Network.
Afterwards, you will be asked to select the mirror, which is the location you will download R from. Select the location nearest you.
Next, select the operating system you are currently working on and wish to install your R version.
Lastly, select the R version you desire and your computer should promptly start downloading the installer.
Once the installer has downloaded, you can run it and install as you would any other application. It is recommended to accept the default options.
Proceed through the installation window by clicking “Continue” or “Next”, accepting the licensing agreement when prompted to do so, selecting the standard installation, and provide an administrative password if applicable. If not, the installation should start and you will receive a notification whether R downloaded successfully or not.
After successfully installing R, you can now download RStudio. RStudio can be downloaded from the RStudio website.
Once on this wesite, you can click on Download
. Then, click on the Download
button that is under RStudio Desktop
, which is the open source license.
Now, you can select your operating system and the RStudio installer should start downloading. Once the download is complete, simply run the application and it should open afterwards.
?
followed by the name of the function or package, or you can use the help()
function.?mean
help(mean)
str()
function.str(iris)
## 'data.frame': 150 obs. of 5 variables:
## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
class()
function.class(iris)
## [1] "data.frame"
install.packages("dplyr")
library()
allows you to load a package into your current R session, which will make all its functions available to use.library(dplyr)
::
after the name of a particular function, you can specify a particular function that you want to use from this package.dplyr::select()
data("iris")
getwd()
function will retrieve the current working directory for your current R session, which is where inputs are found and outputs are sent.getwd()
## [1] "C:/Users/javyr/Desktop"
setwd()
function. Inside this function you will specify the new path that you want R to associate as the current working directory.setwd("C:/Users/javyr/")
getwd()
## [1] "C:/Users/javyr"
c()
.c(2,4,6)
## [1] 2 4 6
2:6
## [1] 2 3 4 5 6
seq(2, 3, by = 0.5)
## [1] 2.0 2.5 3.0
rep(1:2, times = 3)
## [1] 1 2 1 2 1 2
rep(1:2, each = 3)
## [1] 1 1 1 2 2 2
We can manipulate vectors through a series of functions such as sort()
, table()
, rev()
and unique()
.
sort()
will return a sorted version of the inputted vector.
x <- c(1,2,3,4)
sort(x)
## [1] 1 2 3 4
table()
will provide the number of occurences of each distinct value in a vector.table(x)
## x
## 1 2 3 4
## 1 1 1 1
rev()
will reverse the order of values in the inputted vector.rev(x)
## [1] 4 3 2 1
unique()
will output all of the unique values associated with the inputted vector.unique(x)
## [1] 1 2 3 4
# The fourth element.
x[4]
## [1] 4
# All but the fourth.
x[-4]
## [1] 1 2 3
# Elements two to four.
x[2:4]
## [1] 2 3 4
# All elements except two to four.
x[-(2:4)]
## [1] 1
# Elements one and four.
x[c(1,4)]
## [1] 1 4
# Elements which are equal to 10.
x[x == 10]
## numeric(0)
# All elements less than zero.
x[x < 0]
## numeric(0)
# Elements in the set 1, 2, 5.
x[x %in% c(1, 2, 5)]
## [1] 1 2
# Element with name 'apple'.
x["apple"]
## [1] NA
for
and while
loops along with if
statements. In addition, you can create functions.# A for loop that runs a total of 4 times and consists of creating a variable `j`, whose value is the result of adding
# the ith value in the current iteration plus 10 and is redefined for each iteration.
for (i in 1:4){
j <- i + 10
print(j)
}
## [1] 11
## [1] 12
## [1] 13
## [1] 14
# Print the value of `i`, add 1 to its value, and assign the corresponding result to the variable `i`; meanwhile, the
# variable `i`'s value is less than 5.
while (i < 5){
print(i)
i <- i + 1
}
## [1] 4
# Print the word "Yes" if the value of `i` is greater than 3; else, print "No"
if (i > 3){
print("Yes")
} else {
print("No")
}
## [1] "Yes"
# This function will take an input `x`, compute its squared, store the result in variable `squared`, and lastly return
# the result.
square <- function(x){
squared <- x*x
return(squared)
}
# Storing text file in the object `df`
df <- read.table("file.txt")
# Creating a text file based on what's inside object `df`
write.table(df, "file.txt")
# Storing csv file in the object `df`
df <- read.csv("file.csv")
# Creating a csv file based on what's inside object `df`
write.csv(df, "file.csv")
a <- 4
b <- 6
a == b # Are equal
## [1] FALSE
a > b # Greater than
## [1] FALSE
a >= b # Greater than or equal to
## [1] FALSE
is.na(a) # Contains an NA element (missing value)
## [1] FALSE
a != b # Not equal
## [1] TRUE
a < b # Less than
## [1] TRUE
a <= b # Less than or equal to
## [1] TRUE
is.null(a) # Contains a NULL element (value undefined)
## [1] FALSE