# R: Data manipulation

## Basics

### Basic data types

mode(2)          # numeric
mode(pi)
mode(2+3i)       # complex
mode("the lazy dog")  #  character
mode(FALSE)      # logical

### Basic data structures

#### vector

c(1,5,12)
c('a','large','boat')
c(FALSE,TRUE)
1:12           # sequence
seq( from = 1, to = 12, by = 1.5)

#### matrix

mat <- matrix( 1:12, ncol = 3)   # example of assignment, Note: does not print result
mat                              # prints result
( mat2 <- matrix( 1:12, ncol = 3, byrow = T) )   # assignment and printing
rownames(mat) <- c('John','Qing','Tao','Ye')
mat
colnames(mat) <- LETTERS[1:3]
t(mat)

#### array

arr <- array( 1:24, dim = c(2,3,4))
arr
dimnames(arr)3 <- letters[1:4]
arr
dimnames(arr)[c(1,2)] <- list( Row = c('row 1','row 2'), Column = paste( "Col", 1:3))
arr
names( dimnames( arr ) [3]) <- "Panel"
dimnames(arr)
arr
aperm(arr, c(2,1,3))

#### list

Elements can have different modes and different structures
list( 1, 'a', FALSE)

### Exercises

library(car)
data(Prestige)
1. How many occupation have education > 12
2. Let z <- list(a=1:4,b=c(10,13,34), c=4, d=c(NA,2))
1. Find the maximum value in each element of the list
3. Find the mean income for each type of occupation
4. Write a program 'prime(n)' that will find all primes from 1 to n
5. Plot means, standard deviations, and standard errors by group