Variables

Variables allow us to save inbetween constructs or scripts to use them in multiple operations throughout the analysis. Common things to save into variables are:

- tables
- results
- graphs
- constants/settings

Variables in R are of many types:

- integer
- numeric
- complex
- boolean/logical
- character
- vector
- matrix
- list
- (data.frame)

Assignment to variables in R works with <- or since 3.0 with = as well. Both are acceptable, <- is preffered. But I hate it and use = as in every other language. It's up to you, just be consistent!


In [ ]:
num = 5 #works
num2 <- 7 #works, official way - makes R very distinct
num + num2

Names must start with a letter, but can include numbers and underscore _


In [14]:
2hey = "ERROR" #produces error
hey2 = "GOOD"
_broken = 5 #produces error
not_broken = 5


Error in parse(text = x, srcfile = src): <text>:1:2: unexpected symbol
1: 2hey
     ^
Traceback:

Variables are assigned from the right,


In [ ]:
# this actually save the variable, to the pointer a and b
a = "string"
b = 1
# BUT
"string" = b
# will save value of b into the variable string
print(string)

Funnily enough, this also works in R


In [6]:
"char" = "h"
print(char)
# BUT
print("char")

Now let's have some fun.

Numeric

Numeric type is to store floating point numbers. Internally 1 is considered 1.00000...


In [ ]:
i = 5 # numeric
num = 5.1 #numeric
class(pi)

Character

Character type is used to store character vectors. It can store single character as well as longer strings. Each character has lenght of 1. Anything in "" is considered character and is safe. You can save unsafe symbols like + or = inside character easily.


In [3]:
str = "long character" # character vector
class(str)
length(str)


'character'
1

In [4]:
char = 'c' #single quotes should work as well - BEWARE Matlab, C# or and others will kill you for that, stick to ""
class(char)
length(char)


'character'
1

In [5]:
class(5) #see the difference
class("5")  #see the difference


'numeric'
'character'

In [9]:
#We can store unsafe characters in strings
equal_sign = "="
equal_sign


'='

Logical

Logical value can be only TRUE or FALSE (T and F for short). Any comparison leads to logical value.


In [10]:
bool = TRUE
if(bool){
    print("I am always right")
}


[1] "I am always right"

In [11]:
bool = 2 < 3
if(bool){
    print("I am always right")
}
class(bool)


'logical'

Lists

Lists are complex tyeps that can store references to multiple types. Elements can be added


In [ ]:
ls = list(number = 1, text = "Interesing")
ls$number
ls$text
class(ls)

NULL and NA

NULL and NA are special values that R uses. NA is very unique and it stands for Not Available. NULL will eventually became usefull in returning ERROR results.


In [13]:
ls = list(number = 1, text = "Interesing")
ls$telephone
is.null(ls$memory)


NULL
TRUE

CONVERTING VARIABLE TYPES

Usually it's as simple as using as functions (as.numeric, as.character, as.factor), but check often


In [ ]:
num_vec1 = 1:5
as.character(num_vec1)
as.numeric("1")

# Besides characters or numeric values, any variable can also save an R object
# For example, lists are R objects, data.frames are objects, functions etc.