# typecasting
a <- 12321.34124
as.integer(a)
a
12321
a <- 1
b <- 2
a + b
sum(a,b)
sum(a,b, na.rm=T) #remove not applicable values so there's no runtime error.
a - b
a * b
prod(a,b)
a/b
a^b
typeof(a+b) # same as instanceOf(a) in java
# Strings
word1 <- "hello"
word2 <- "world"
word1
'hello'
'world'
paste() # binary operator on strings to add and output one string
c() # put things in a box together, like an array of type strings
word3 <- paste(word1, word2)
word3
'hello world'
word4 <- c(word1, word2)
word4
'hello' . 'world'
# change the seperator
word6 <- paste(word1, word2, sep="")
word6
'helloworld'
See that there's a difference between paste and c
length(word3)
1
length(word4)
2
names <- c("Alice", "Bob")
ages <- c(30, 25)
names
'Alice' . 'Bob'
30 . 25
paste(names, "is", ages, "years old")
'Alice is 30 years old' . 'Bob is 25 years old'
a <- 5
b <- 3
x <- a > b
x
TRUE
y <- a < b
y
FALSE
# you have == as well as !=. Also = where = means <- like python assignments and java.
typeof(a)
'double'
typeof(x)
'logical'
typeof(a > b)
'logical'
n <- c(1,2,3,4,5)
l <- c("a","b","c","d","e")
length(n) == length(l)
TRUE
t <- c(T,F,T)
# Access List / Array
n[2]
2 # NOT ZERO INDEXED
l[5]
'e'
l[6]
NA
Create a subset:
n[n > 3]
4 . 5
n[n != 3]
1 . 2 . 4 . 5
Data hierarchy:
Lowest category wins
a = c(3.14, "pi")
a
'3.14' . 'pi'
typeof(a)
'character'
b = d(T, 42)
b
1 . 42
typeof(b)
'numeric'
You also have as.numeric() to your disposal too.
a <- T + T
b <- T + F
a
2
b
1
If you don't want R to auto convert your data in the array, create a list.