Download R

R is a free software environment for statistical computing and graphics. See the R project homepage for more information. Download r here.


Download RStudio

RStudio is a free and open-source integrated development environment (IDE) for R. It provides a graphic interface and user-friendly environment for r programmers to write and test their code. Download RStudio here. RStudio is not the only IDE for r, but it is the most widely used.


Base R

R has hundreds of native functions that are part of the base package. These are the functions which allow R to perform as a programming language, including basic arithmetic and various programming inputs/outputs.

Here are some mathematical operators that are automatically recognized by r.

Description Operator
Addition +
Subtraction -
Multiplication *
Division /
Square Root sqrt(x)
Absolute Value abs(x)
Exponentiation ^
Natural Exponential Function exp(x)
Natural Logarithm log(x)
Common Logarithm log10(x)

example

4*4
## [1] 16

Here are some comparative operators that are automatically recognized by r.

Description Syntax
Less than VariableName < Value
Less than or Equal to VariableName <= Value
Greater than VariableName > Value
Greater than or Equal to VariableName >= Value
Equal to VariableName == Value
Not Less than !VariableName < Value
Not Less than or Equal to !VariableName <= Value
Not Greater than !VariableName > Value
Not Greater than or Equal to !VariableName >= Value
Not Equal to !VariableName = Value

example

4>3
## [1] TRUE
These do not even scratch the surface of what commands and functions are native to r as part of the base package.. Here is a helpful cheatsheet from the folks at R Studio.



Packages

Because r is an open source platform, users and developers from all over the world are able to contribute to r by creating packages that can be utilized by other r users. A Package is a suite of pre-defined commands, data, and code that serve as toolkits for simplifying various programming tasks.

If you want to produce beautiful visualizations, scrape tweets from twitter, manipulate data quickly and efficiently, perform statistical analysis without having to write out entire complex algorithms, or do just about anything in r, then you will likely find yourself becoming familiar with various packages very soon after starting to use r.


Installing packages

In order to use a package, you will need to install it. A package is installed as follows:

install.packages("stringr")

This only ever needs to be done once per package, unless you are installing an updated version of a package after a new release.


Loading packages for use

Each time R is restarted, you will need to load the packages that you intend to use in the current session. A package is loaded as follows:

library(stringr)


Package Help

If you type a “?” before the name of an installed package, you will pull up the help page for the given package.

?stringr


Using a package

The stringr package that we’ve installed above contains dozens of useful functions for dealing with text strings. For Example, the str_detect() function from stringr gives us a true or false value based on whether or not a specified string pattern appears in a body of text.

#str_detect(string, pattern)  This is the syntax for the str_detect function

str_detect("Mary had a little lamb", "Mary")  #example
## [1] TRUE



References

R Core Team (2016). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria. URL https://www.R-project.org/.

RStudio Team (2015). RStudio: Integrated Development for R. RStudio, Inc., Boston, MA URL http://www.rstudio.com/.

Hadley Wickham (2017). stringr: Simple, Consistent Wrappers for Common String Operations. R package version 1.2.0. https://CRAN.R-project.org/package=stringr



Questions & Feedback

Please feel free to leave feedback that could help improve this site. If you have questions, please leave them below as well and I will do my best to support you as soon as possible.

Comment Box is loading comments…