# R: Data manipulation

### From MathWiki

**Quick links**

- Data manipulation and data mining: http://www.togaware.com/datamining/

Table of contents |

[edit]

## Basics

[edit]

### Basic data types

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

[edit]

### Basic data structures

[edit]

#### vector

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

[edit]

#### 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)

[edit]

#### 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))

[edit]

#### list

- Elements can have different modes and different structures

list( 1, 'a', FALSE)

[edit]

### Basic language

[edit]

### Exercises

library(car) data(Prestige)

- How many occupation have education > 12
- Let z <- list(a=1:4,b=c(10,13,34), c=4, d=c(NA,2))
- Find the maximum value in each element of the list

- Find the mean income for each type of occupation
- Write a program 'prime(n)' that will find all primes from 1 to n
- Plot means, standard deviations, and standard errors by group

[edit]

## More advanced

[edit]

### Exercises

[edit]

## Even more advanced

[edit]

### Exercises

[edit]

## Notes on Spector *Data Manipulation with R*

Phil Spector's excellent book explains how to do many of the manipulations for which special functions were created in 'fun.R' (www.math.yorku.ca/~georges/R/fun.R). The following notes describe the approach used in Spector's book and identify functions that are still lacking in base R.