| Name | Description | Examples |
|---|---|---|
| Logical | binary | TRUE, FALSE |
| Integer | round numbers | 1L, 45L |
| Numeric | any number | 1.234, 1e+3 |
| Character | text | "R rocks!" |
| Complex | complex numbers | 1 + 2i |
| Raw | bytes | 41 5a |
seq( from = 3, to = 5, by = .5) ## [1] 3.0 3.5 4.0 4.5 5.0
function()my_fun = function( x, y = 1 ){
z = x^2
return(z + y)
}
my_fun(x=3)
## [1] 10
help(fun) or ? fun to get documentationrep( x = "blah", times = 2)
## [1] "blah" "blah"
rep( x = rep("ha", 2), times = 2)
## [1] "ha" "ha" "ha" "ha"
c() concatenation function
<- or =)c(0, 1, 2)
## [1] 0 1 2
my_first_vector <- c("a", "b", "c")
my_first_vector
## [1] "a" "b" "c"
class function identifies object typeclass( c(0, 1, 2) ) ## [1] "numeric"
class( c(TRUE, 3.14, "R") ) ## [1] "character"
| Value | Description | Use |
|---|---|---|
| NA | not available | missing value |
| NULL | null object | represent "nothing" |
| (+/-)Inf | infinity numbers | 1/0 |
| NaN | not a number | 0/0 |
c(NA, NULL, Inf, Inf/Inf) ## [1] NA Inf NaN
| Operator | Description | Example |
|---|---|---|
+ |
addition | 1 + 1 (=2) |
- |
subtraction | 1 - 1 (=0) |
* |
multiplication | 2 * 3 (=6) |
/ |
division | 4 / 2 (=2) |
^ |
exponentiation | 2^3 (=8) |
%% |
modulo | 5 %% 2 (=1) |
%/% |
integer division | 5 %/% 2 (=2) |
(1:6) + (1:2) ## [1] 2 4 4 6 6 8
f <- factor( c("male", "female", "female", "male") )
class(f)
## [1] "factor"
typeof(f) # object's storage type
## [1] "integer"
str(f) # object's structure
## Factor w/ 2 levels "female","male": 2 1 1 2
[ ]x <- c("a", "b", "c")
x[2]
## [1] "b"
x[1:2]
## [1] "a" "b"
x[ c(T, F, T) ]
## [1] "a" "c"
| Operator | Description | Example |
|---|---|---|
< (<=) |
less (less or equal) to | 3 < 2 (=FALSE) |
> (>=) |
greater (greater or equal) to | 2 >= 2 (=TRUE) |
== |
exactly equal to | 1 == 1 (=TRUE) |
!= |
not equal to | 1 != 1 (=FALSE) |
%in% |
belongs to | 1 %in% (1:3) (=TRUE) |
| Operator | Description | Example |
|---|---|---|
! |
logical negation | !TRUE (=FALSE) |
| |
logical OR | TRUE | FALSE (=TRUE) |
& |
logical AND | TRUE & FALSE (=FALSE) |
| any | multiple OR | any(T, F, F) (=TRUE) |
| all | multiple AND | all(T, F, F) (=FALSE) |
matrix( data = 1:6, nrow = 3 ) ## [,1] [,2] ## [1,] 1 4 ## [2,] 2 5 ## [3,] 3 6 matrix( 1:6, ncol = 3, byrow = TRUE ) ## [,1] [,2] [,3] ## [1,] 1 2 3 ## [2,] 4 5 6
matrix(1:4, 2) ## [,1] [,2] ## [1,] 1 3 ## [2,] 2 4 matrix(1:4, 2) [1, ] ## [1] 1 3
mat <- matrix(1:6, nrow = 3)
rownames(mat) <- paste(1:3)
colnames(mat) <- c("a", "b")
mat
## a b
## 1 1 4
## 2 2 5
## 3 3 6
mat["1","a"]
## [1] 1
list() functionmy_list <- list("a", 1:3, NULL, NA)
class(my_list)
## [1] "list"
my_list
## [[1]]
## [1] "a"
##
## [[2]]
## [1] 1 2 3
##
## [[3]]
## NULL
##
## [[4]]
## [1] NA
str()my_list <- list( num = 1:5, let = c("a", "b"), log = c(T, F, T) )
str(my_list)
## List of 3
## $ num: int [1:5] 1 2 3 4 5
## $ let: chr [1:2] "a" "b"
## $ log: logi [1:3] TRUE FALSE TRUE
names(my_list)
## [1] "num" "let" "log"
[ ][[ ]], or by name with $my_list <- list( num = 1:5, let = c("a", "b"), log = c(T, F, T) )
my_list[1]
## $num
## [1] 1 2 3 4 5
my_list[[1]]
## [1] 1 2 3 4 5
my_list$num
## [1] 1 2 3 4 5
my_df <- data.frame( age = c(21, 38, 41), sex = c("M", "F", "M") )
str(my_df)
## 'data.frame': 3 obs. of 2 variables:
## $ age: num 21 38 41
## $ sex: Factor w/ 2 levels "F","M": 2 1 2
dim(my_df)
## [1] 3 2
if( runif(1) >= .5 ){
print("Heads")
}else{ print("Tails") }
## [1] "Heads"
for(i in c("a", "b")){
print(i)
}
## [1] "a"
## [1] "b"