### Vector

```
# c = cell/column
a <- c(1,2,4.3,-9,111) # numeric vector
b <- c("one", "two", "three") # character vector
c <- c(TRUE,TRUE,TRUE,FALSE) # logical vector
a # print vector a
```

`## [1] 1.0 2.0 4.3 -9.0 111.0`

`print(a) # print vector a`

`## [1] 1.0 2.0 4.3 -9.0 111.0`

`a[c(2,4)] # 2nd and 4th elements of vector`

`## [1] 2 -9`

### Matrix

```
# matrix
y <- matrix(1:20, nrow=5, ncol=4)
# or another way to build matrix:
cells <- c(1,26,24,68)
rnames <- c("R1","R2")
cnames <- c("C1","C2")
mymatrix <- matrix(cells, nrow=2, ncol=2, byrow=TRUE, dimnames=list(rnames, cnames))
mymatrix
```

```
## C1 C2
## R1 1 26
## R2 24 68
```

```
# identify rows, columns or elements using subscripts.
y[,4] # 4th column of matrix
```

`## [1] 16 17 18 19 20`

`y[3,] # 3rd row of matrix`

`## [1] 3 8 13 18`

`y[2:4,1:3] # rows 2,3,4 of colums 1,2,3`

```
## [,1] [,2] [,3]
## [1,] 2 7 12
## [2,] 3 8 13
## [3,] 4 9 14
```

### Array

Arrays are similar to matrices, but can have more than two dimensions.

`help(array)`

### Data Frame

Data frame is more general than a matrix, in that different that columns can have diffrent modes (numeric, character, factor etc.)

```
d <- c(1,2,3,4)
e <- c("red", "blue", "green", NA)
f <- c(TRUE,TRUE,TRUE,FALSE)
mydata <- data.frame(d,e,f)
names(mydata) <- c("ID","Color","Passed") # Variable names
print(mydata)
# there are a variety ways to identify the elements of a data frame:
mydata[1:2] # column 1,2 of data frame
mydata[c("ID","Passed")] # columns ID, Passed from data frame
mydata$ID # variable ID in the data frame
```

### List

An ordered collection of objects (components). A list allows you to gather a variety of (possible unrelated) object under one name.

```
# Example of a list with 4 components: string, numeric vector, matrix and scaler
w <- list(name="Fred", mynumbers=a, mymatrix=y, age=5.3)
print(w)
# example list containing two lists:
list1 <- w
list2 <- list(name="John", mynumbers=a, mymatrix=y, age=8.1)
v <- c(list1,list2)
print(v)
# identify elements of using [[]] convention
list1[[2]] # 2nd component of the list
list1[["age"]]
```

### Factor

Tell R that a variable is nominal by making it a factor. The factor stores the nominal values as a vector of integers in the range `[1...k]`

(where `k`

is the number of unique values in the nominal variable), and an internal vector of character strings (the original values) mapped to these integers.

Variable gender with 20 “male” entries and 30 “female” entries. This stores gender as 20 1s and 30 2s and associates 1=female, 2=male internally (alphabetically).

```
gender <- c(rep("male",20), rep("female",30))
gender <- factor(gender)
print(gender)
```

```
## [1] male male male male male male male male male male
## [11] male male male male male male male male male male
## [21] female female female female female female female female female female
## [31] female female female female female female female female female female
## [41] female female female female female female female female female female
## Levels: female male
```

`summary(gender) # R now treats gender as a nominal variable.`

```
## female male
## 30 20
```

An ordered factor is used to represent ordinal variable. Variable rating coded as “large”, “medium”, “small”

```
rating <- c(rep("pieni",5),rep("keski",5),rep("iso",5))
rating <- ordered(rating)
print(rating)
# recodes rating to 1,2,3
summary(rating)
```

### Testing vector and datatypes

```
library(purrr)
# Different datatypes:
v_log <- c(TRUE, FALSE, FALSE, TRUE)
is_atomic(v_log)
```

`## [1] TRUE`

`v_log[5]`

`## [1] NA`

`v_log[1:5]`

`## [1] TRUE FALSE FALSE TRUE NA`

```
# Example 1
v_int <- 1:4
is_integer(v_int)
```

`## [1] TRUE`

`is.numeric(v_int)`

`## [1] TRUE`

`v_int[5]`

`## [1] NA`

```
# Example 2
v_doub <- 1:4 * 1.2
is_double(v_doub)
```

`## [1] TRUE`

`is.numeric(v_doub)`

`## [1] TRUE`

`is.integer(v_doub)`

`## [1] FALSE`

`v_doub[5]`

`## [1] NA`

```
# Example 3
v_char <- letters[1:4]
is_character(v_char)
```

`## [1] TRUE`

```
# Example 4
is.character(append(c(1,2,3), c("new")))
```

`## [1] TRUE`

### Bubba-example

```
bubba <- list(first="one", second="two", third="three")
bubba
class(bubba)
# add something to class
class(bubba) <- append(class(bubba), "Transparent")
bubba
class(bubba)
GetFirst <- function(x){
UseMethod("GetFirst",x)
}
GetFirst.Flamboyancy <- function(x){
return(x$first)
}
GetFirst.Transparent <- function(x){
return(x$second)
}
GetFirst(bubba)
UseMethod("GetFirst",bubba)
```