Saturday, March 12, 2011

Zuur: Chapter 2: Getting data into R


 

First Steps in R

-          typing in small datasets

o   type it in as scalars – variables containing a single value

o   concatenating data with c Function

-          Missing data – indicated by NA

o   default ; na.rm = FALSE

§  causes R function to return NA

o   change

§  sum(Head, na.rm = TRUE)

·         sum of remaining values is returned

-          combining variables with c, cbind and rbind functions

o   example

§  BirdData <- c (Wingcrd, Tarsus, Head, Wt)

§  single vector

-          rep()

o   example

§  Id <-  rep(c(1,2,3,4), each = 8)

§  Id <- re(1:4), each = 8)

§  a <- seq(from = 1, to = 4, by = 1)

-          c function – way of combining data or variables

-          cbind – combines variables so that output contains original variables in columns

o   z[, 1] à first column

§  or  z [1:8, 1]

o   x[2,] à second row

§  or z [2, 1:4]

-          dim(Z) – dimensions of

-          rbind à similar to cbind, but values displayed in rows

-          Combining data with vector function

o   W <- vector(length = 8)

o   W[1] <- 59

o   etc

o   advantage – can define how many elements variable should have

-          Combining data using matrix

o   Dmat <- matrix(nrow = 8, ncol = 4)

§  input data          

·         Dmat [,1] <- c( ……..)

§  colnames(Dmat) <- c("Name1", etc]

o   Dmat2 <- as.matrix (cbind( Wingcrd, Tarsus, Head, Wt))

-          Combining data with data.frame Function

o   Dfrm <- data.frame (WC = Wingcrd, TS = Tarsus, HD = Head, W = WT)

o   combine variables of equal length, with each row in data frame containing observations on the same sampling unit

o   data frame – creates an object, and within it, stores values of the 4 morphometric variables

§  advantage, can make changes to data without affecting original data

-          Combining data using List function

o   black box

o   list y à all information contained in is accessible

§  Y$x2, for example, where x2 is a component

o   nearly all functions in R produce output that is stored in a list

 

 

Importing Excel Data

 

-          1 -> prepare data in excel, export to tab delimited , use read.table()

-          prepare data in excel

-          export data to tab delimited file ascii file

-          use read.table()

o   squid <- read.table(file = "C:\\RBook\\squid.txt", header = TRUE)

-          note multiple options.

 

 

-          setwd("C:\\RBook\\")    à set wd

-          check wd: getwd()

-           

 

-          scan

 

o   read.table à stores data in data frame

o   scan à stores data as matrix

o   scan à works faster à god for large data sets à all data must be numerical

 

 

Accessing Data from Other Statistical Packages

 

 

 

Accessing a Database

 

 

 

 

 

 

No comments: