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
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.
In [ ]:
i = 5 # numeric
num = 5.1 #numeric
class(pi)
In [3]:
str = "long character" # character vector
class(str)
length(str)
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)
In [5]:
class(5) #see the difference
class("5") #see the difference
In [9]:
#We can store unsafe characters in strings
equal_sign = "="
equal_sign
In [10]:
bool = TRUE
if(bool){
print("I am always right")
}
In [11]:
bool = 2 < 3
if(bool){
print("I am always right")
}
class(bool)
In [ ]:
ls = list(number = 1, text = "Interesing")
ls$number
ls$text
class(ls)
In [13]:
ls = list(number = 1, text = "Interesing")
ls$telephone
is.null(ls$memory)
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.