Workshop 2.1: Data frames Murray Logan 15 Jul 2017
Section 1 Data importation and exportation
Prior preparation Download the macnally.csv file • www.flutterbys.com.au/stats/ downloads/data/macnally.csv • put it in a directory you wish to work from Make sure you know where you have put it!
> download.file ('http://www.flutterbys.com.au/stats/downloads/data/macnally.csv', + '~/macnally.csv') Prior preparation Download the macnally.csv file • www.flutterbys.com.au/stats/ downloads/data/macnally.csv • put it in a directory you wish to work from OR
> getwd () [1] "/home/murray/Work/SUYR/downloads/slides" Working directory • Querying the current working directory
> getwd () [1] "/home/murray/Work/SUYR/downloads/slides" > #Go to a subdirectory of the current directory > setwd ('data') > setwd ('..') > setwd ('../data') Working directory • Querying the current working directory • Examples of navigating (moving current working directory) > #Go to the parent directory > #Go to a sibling directory
Section 2 Working with files
4.3 2.9 4.7 3.6 6.5 2.5 Box-Ironbark Bailieston 6.5 9.7 Gipps.Manna Bittern 4.1 4.9 Mixed Balnarring 6.0 4.9 Mixed Cape Schanck Foothills Woodland 14.0 5.6 Nepean Foothills Woodland Mixed St Andrews 1.7 2.6 Mixed Hawke 5.6 3.3 Mixed Minto 3.4 2.7 Mixed Ghin Ghin 9.6 4.0 Mixed OShannassy 3.8 3.8 Donna Buang 1.5 0.0 Panton Gap 3.0 0.0 Foothills Woodland Tallarook 7.1 2.2 Box-Ironbark Costerfield 3.0 1.6 Mixed Waranga 2.6 0.0 Box-Ironbark Sayers 2.1 1.1 Box-Ironbark Rushworth River Red Gum Upper Yarra River Red Gum Mixed 4.7 3.1 Gembrook Mixed 7.5 7.5 Arcadia 3.1 0.0 Toolamba Undera River Red Gum 2.7 0.0 Coomboona River Red Gum 4.4 0.0 Montane Forest 3.1 9.3 Gipps.Manna 3.4 9.2 5.6 5.6 Mixed Lysterfield 3.0 5.0 Gipps.Manna Cranbourne 8.4 3.8 Mixed Warneet Gipps.Manna Mixed Pearcedale 3.4 0.0 Mixed Reedy Lake GST EYR HABITAT > MACNALLY header=T, row.names=1, strip.white=TRUE) + '/home/murray/Work/SUYR/downloads/data/macnally.csv', Red Hill 8.1 4.1 > MACNALLY <- read.csv ( Heathcote Ju Ben Cairn 5.4 6.5 Mixed Millgrove 3.8 6.5 Montane Forest Warburton 3.7 2.5 Montane Forest 4.6 1.2 Devilbend Foothills Woodland Sherwin 3.2 5.2 Montane Forest Fern Tree Gum 4.6 5.3 Mixed Olinda 8.3 7.1 Mixed + Importing from text file e f i l e d r a t e p a a s o m m C 1. Full path
4.3 2.9 4.7 3.6 6.5 2.5 Box-Ironbark Bailieston 6.5 9.7 Gipps.Manna Bittern 4.1 4.9 Mixed Balnarring 6.0 4.9 Mixed Cape Schanck Foothills Woodland 14.0 5.6 Nepean Foothills Woodland Mixed St Andrews 1.7 2.6 Mixed Hawke 5.6 3.3 Mixed Minto 3.4 2.7 Mixed Ghin Ghin 9.6 4.0 Mixed OShannassy 3.8 3.8 Donna Buang 1.5 0.0 Panton Gap 3.0 0.0 Foothills Woodland Tallarook 7.1 2.2 Box-Ironbark Costerfield 3.0 1.6 Mixed Waranga 2.6 0.0 Box-Ironbark Sayers 2.1 1.1 Box-Ironbark Rushworth River Red Gum Upper Yarra River Red Gum Mixed 4.7 3.1 Gembrook Mixed 7.5 7.5 Arcadia 3.1 0.0 Toolamba Undera River Red Gum 2.7 0.0 Coomboona River Red Gum 4.4 0.0 Montane Forest 3.1 9.3 Gipps.Manna 3.4 9.2 5.6 5.6 Mixed Lysterfield 3.0 5.0 Gipps.Manna Cranbourne 8.4 3.8 Mixed Warneet Gipps.Manna Mixed Pearcedale 3.4 0.0 Mixed Reedy Lake GST EYR HABITAT > MACNALLY [1] "/home/murray/Work/SUYR/downloads/slides" > MACNALLY <- read.csv ('../data/macnally.csv', Red Hill 8.1 4.1 + Heathcote Ju Ben Cairn 5.4 6.5 Mixed Millgrove 3.8 6.5 Montane Forest Warburton 3.7 2.5 Montane Forest 4.6 1.2 Devilbend Foothills Woodland Sherwin 3.2 5.2 Montane Forest Fern Tree Gum 4.6 5.3 Mixed Olinda 8.3 7.1 Mixed header=T, row.names=1, strip.white=TRUE) Importing from text file e f i l e d r a t e p a a s o m m C 2. Relative path > getwd () #to see the current working directory
4.3 2.9 Foothills Woodland 14.0 5.6 Donna Buang 6.5 2.5 Box-Ironbark Bailieston 6.5 9.7 Gipps.Manna Bittern 4.1 4.9 Mixed Balnarring 6.0 4.9 Mixed Cape Schanck Nepean 1.5 0.0 4.7 3.6 Foothills Woodland St Andrews 1.7 2.6 Mixed Hawke 5.6 3.3 Mixed Minto 3.4 2.7 Mixed Ghin Ghin 9.6 4.0 Mixed Mixed Upper Yarra 3.8 3.8 3.0 0.0 Foothills Woodland Tallarook 7.1 2.2 Box-Ironbark Costerfield 3.0 1.6 Mixed Waranga 2.6 0.0 Box-Ironbark Sayers 2.1 1.1 Box-Ironbark Rushworth River Red Gum Mixed Toolamba 4.4 0.0 River Red Gum Coomboona 2.7 0.0 River Red Gum Undera 3.1 0.0 River Red Gum Arcadia 7.5 7.5 Mixed Gembrook 4.7 3.1 OShannassy Montane Forest Gipps.Manna Gipps.Manna Mixed Red Hill 5.6 5.6 Mixed Lysterfield 3.0 5.0 Panton Gap Cranbourne 8.4 3.8 Warneet Devilbend 3.4 9.2 Gipps.Manna Pearcedale 3.4 0.0 Mixed Reedy Lake GST EYR HABITAT > MACNALLY header=T, row.names=1, sep='\t', strip.white=TRUE) 8.1 4.1 Mixed > MACNALLY <- read.table ('../data/macnally.txt', 3.7 2.5 3.1 9.3 Mixed Ben Cairn 5.4 6.5 Mixed Millgrove 3.8 6.5 Montane Forest Warburton Montane Forest 8.3 7.1 Heathcote Ju 4.6 1.2 Foothills Woodland Sherwin 3.2 5.2 Montane Forest Fern Tree Gum 4.6 5.3 Mixed Olinda + Importing from text file e f i l e d r a t e p a b s T a Relative path
+ > write.table (MACNALLY, '../data/macnally.csv', quote=FALSE, row.names=TRUE, sep=',') Exporting to a text file
R and Excel?
Mixed 8.1 4.1 1 Cranbourne Gipps.Manna 3.0 5.0 5 Lysterfield Mixed 5.6 5.6 6 Red Hill Mixed 8.1 4.1 > ##OR > library (gdata) LOCATION HABITAT GST EYR Reedy Lake Warneet Gipps.Manna 8.4 3.8 Mixed 3.4 0.0 2 Pearcedale Gipps.Manna 3.4 9.2 3 Warneet Gipps.Manna 8.4 3.8 4 Cranbourne Gipps.Manna 3.0 5.0 5 Lysterfield Mixed 5.6 5.6 6 Red Hill 4 3 Pearcedale Gipps.Manna 3.4 9.2 > wb= loadWorkbook ("../data/macnally.xlsx") 2 Mixed 3.4 0.0 Reedy Lake 1 HABITAT GST EYR LOCATION > library (XLConnect) > macnally= readWorksheet (wb,sheet="Sheet1",header=TRUE) R and Excel? l x c e m E f r o n g a d i R e > head (macnally) > macnally<- read.xls ('../data/macnally.xlsx',sheet='Sheet1',header=TRUE) > head (macnally)
> wb= loadWorkbook ("../data/macnally1.xlsx", create=TRUE) > library (XLConnect) R and Excel? c e l E x t o i n g r i t W > createSheet (wb, name='MacNally') > writeWorksheet (wb, macnally, sheet='MacNally') > saveWorkbook (wb)
> save (MACNALLY, meanGST, file='macnallystats.RData') > save (MACNALLY, file='../data/macnally.RData') > meanGST > meanGST <- mean (MACNALLY$GST) > #calculate the mean GST Saving R objects t j e c o b u a l v i d n d i n i g a v i n S a s e c t o b j e i p l u l t g m v i n S a > #display the mean GST > #save the MACNALLY data frame as well as the mean GST object
> load (file='../data/macnally.RData') Loading R objects
Recommend
More recommend