Loading [MathJax]/extensions/tex2jax.js

Translate

Wednesday, February 19, 2020

Primer on the R Language

Disclaimer: This material is based on my notes during an R course. It also includes content I consider important for starting. The original material may be found in a GITLAB repository.

Introducction and Basic Interface Commands

Clean a session of R: Ctrl + L
Save a script,in Mac OS Command+S; Linux/Windows Ctrl+S
A command may be interrupted using Esc o Ctrl+C You may want to interrupt a command when there is a loop that just won’t finish
Define the workspace (In RStudio you can also use Session > Set Working Directory > Choose directory)
setwd("PATH")
#Insert the path of your workspace
To run a selected line: In Mac, Command+Enter. In Linux or Windows Ctrl+Enter.

Using R for arithmetical calculations

1+100
## [1] 101
The order of arithmetical operations is respected
(3+(5*(2^2)))
## [1] 23
3 + 5 * 2^2
## [1] 23
1/100000
## [1] 1e-05
Exponential notation may be used
3e2
## [1] 300
3e-2
## [1] 0.03

Trigonometrical functions

Trigonometrical functions are incorporated
          cos(x)
          sin(x)
          tan(x)
          acos(x)
          asin(x)
          atan(x)
For default you must use radians
sin(pi/2)
## [1] 1

Logarithms

The function log() is a natural logarithm Be noticed that the number e, is written as exp(1) in the R language
log(exp(1))
## [1] 1
For a logarithm of base ’n’, use logn()
log2(2)
## [1] 1

Rational Operators

          ‘>’ More than
          ‘<’ Less than
          ‘<=’ Less or equal than
          ‘>=’ More or equal than
          ‘==’ Equal
          ‘!=’ Different
4 != 4
## [1] FALSE

Logical Operators

          ‘!’ Logical negation
          ‘&’ Logical conjunction by element
          ‘&&’ Logical conjunction
          ‘||’ Logical disjunction

Variable Assignation

Use the operator ‘<- o:p="">
x <- span=""> 9 #Assigning 9 to the value of x
#Check the value of x
## [1] 9
x <- span="">1/3
y <-x span="">+1

Basic rules for variable assignation

a.        They can’t start with numbers or ’_’
b.        There should not be blank spaces within the name

Vectorization

1:10 #Create a vector with all whole numbers from 1 up to 10
##  [1]  1  2  3  4  5  6  7  8  9 10
1+1:10 #Sum 1 to each member of the vector
##  [1]  2  3  4  5  6  7  8  9 10 11
var<- span="">1:10
var
*2 #Multiply by 2 every member of the vector
##  [1]  2  4  6  8 10 12 14 16 18 20
Consult the variables created
ls()
Delete elements
rm()
Example:
t=1
rm(t)

Administrating packages

Check for installed packages
installed.packages()
Install package
install.packages()
Update packages
update.packages()
Delete package
remove.packages()
Download package
install.packages()
”Activate” a package
library()
Search for help in R
-->
help()

No comments:

Post a Comment