SESSION ONE

Table of Contents

1 SESSION ONE.

1.1 Data in R. Reading and writing data in R.

There are two most important attributes of data in R- the mode and the class.

Modes: numeric (1, 2, 3), character (‘one’,’two’,’three’), logical (TRUE/FALSE)

Modes of a data object: data.frame and list (allow multiple modes within single object), matrix (all data is in the same mode)

Class: numeric, character, factor (categorical data).

How to find out what kind of data do you have?

Use functions mode, class, typeof (Example will be given later).

1.1.1 How to enter, store and manipulate with data in R.

Create a table in R.

This table as an R object will contain several modes: characters and numbers.

GenusA1A2A3
Glomus51040
Gigaspora753
Paraglomus0110

r1<-c ('Glomus', 5, 10,40)

Check the class and mode of the object using sapply function:

sapply(r1,class) ###you give first your data (r1), then the function that you are going to apply to your data (class or mode)

r2<-c ('Gigaspora', 7, 5, 3)

r3<-c ('Paraglomus', NA, 1, 10)

t<-rbind(r1,r2,r3)

To see your table type the name that you gave to your table:

t

If you have very long table use function head to see first five rows:

head(t)

Check the mode of your table:

typeof(t)

t1<-data.frame(t)

Another way to do it:

t2<-data.frame(r1,r2,r3)

t3<-t(t2) ###function t melts your table - change rows and columns

typeof(t3)

t3<-data.frame(t3)

1.1.2 How to name rows and columns.

Give the names to your columns:

colnames(t1)<-c('genus','A1','A2','A4')

colnames(t1) ##shows you the names of the columns in your table

Now rename the last column to A3:

colnames(t1)[4]<-c('A3')

colnames(t1)

Function rownames do the same for rows.

rownames(t1)<-c('Glomus','Gigaspora','Paraglomus')

1.1.3 How to check dimensions of the table:

dim(t1)

> dim(t1)

[1] 3 4 ######first number shows you how many rows, second columns

1.1.4 How to see particular row or column from your table:

for example, column 2

t1[,2] ######type the name of your table,square brackets, comma, number of the column

or t1$A1 ####column name, $, the name of the column

or t1(,c(2))

row 1

t1[1,] (t1$Glomus WILL NOT work for rows)

Show the columns 1 and 4

t1[,c(1,4)]

Show columns from 2 to 4:

t1[,c(2:4)]

1.1.5 How to delete row or column:

Delete first column.

t1[,-c(1)] ###the result won't be stored in R and if you now type t1 you'll still have the column you deleted in your table

To store the new table you have to assign a new to name to the operation above:

t5<-t1[,-c(1)]

t5

If you want to delete row:

t1[-c(1),]

Delete columns from 1 to 3:

t1[,-c(1:3)]

Delete rows from 1 and 3:

t1[-c(1,3),]

1.1.6 How to add rows or columns to the table: use functions rbind to add rows and cbind to add columns.

Convert your table to this table:

A1A2A3plant
Glomus51040CA
Gigaspora753TO
Paraglomus0110TO
Scutellospora321CA

plant<-c('CA', 'TO', 'TO')

t6<-data.frame(t5,plant)

Check the mode of your data using summary function:

summary(t6)

1.1.7 How to convert one mode to another

Convert columns 1 to 3 to numeric, and the last to factor:

t6$A1 <- as.numeric(t6$A1)

t6$A2 <- as.numeric(t6$A2)

t6$A3 <- as.numeric(t6$A3)

t6$plant <- as.factor(t6$plant)

summary(t6)

Thats what you should see at the end:

A1 A2 A3 plant

Min. :1.0 Min. :1.0 Min. :1.0 CA:1

1st Qu.:1.5 1st Qu.:1.5 1st Qu.:1.5 TO:2

Median :2.0 Median :2.0 Median :2.0

Mean :2.0 Mean :2.0 Mean :2.0

3rd Qu.:2.5 3rd Qu.:2.5 3rd Qu.:2.5

Max. :3.0 Max. :3.0 Max. :3.0

Scutellospora<-c(3,32,11,'CA')

t7<-data.frame(rbind(t6,Scutellospora))

rownames(t7)[4]<-c('Scutellospora')

summary(t7)

t7$A1 <- as.numeric(t7$A1)

t7$A2 <- as.numeric(t7$A2)

t7$A3 <- as.numeric(t7$A3)

Convert one of the column to the factor, and then back to numeric

t7[,3] <- as.factor(t7[,3])

summary(t7)

t7[,3] <- as.numeric(t7[,3])

summary(t7)

1.1.8 How to save created table.

You need to save table t7 as R data and as csv file.

  • Saving R data:

    save(t7,file = 'table.rdata') ######this file can be opened in R only

  • How to load this data when you start a new session:

    load('table.rdata')

    function ls() will show you the objects that this file has

  • Saving CSV file:

    write.csv(t7, file ='table.csv')

  • How to read CSV file:

    d<-read.csv('table.csv', header=T)

  • Saving as a text file:

    write.table(t7, file ='table.txt', row.names = T, col.names = T, sep = ",")

  • How to read table from text file:

    d<-read.table('table.txt',sep =",")

    For additional information see Phil Spector. Data manipulation with R. Chapters 1,2

Author: ELVIRA <kurmaevaer@titan.sfasu.edu>

Date: 2009-06-19 12:18:10 CDT

HTML generated by org-mode 6.09a in emacs 23