Top Banner
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing The Data Frame, Type Conversion, Graphing A. Michelle Lawing Ecosystem Science and Management Texas A&M University College Sta,on, TX 77843 [email protected] Adapted from Gene Hunt Session 1 and 2 hYp://paleobiology.si.edu/staff/individuals/hunt.cfm
15

The$DataFrame,$Type$Conversion,$ Graphing$people.tamu.edu/~alawing/materials/ESSM689/DataFrame.pdf · ESSM689$Quan,tave$Methods$in$ Ecology,$Evolu,on$and$Biogeography$...

Oct 05, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: The$DataFrame,$Type$Conversion,$ Graphing$people.tamu.edu/~alawing/materials/ESSM689/DataFrame.pdf · ESSM689$Quan,tave$Methods$in$ Ecology,$Evolu,on$and$Biogeography$ Ecosystem$Science$and$Management|$

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

The  Data  Frame,  Type  Conversion,  Graphing  

A.  Michelle  Lawing  Ecosystem  Science  and  Management  Texas  A&M  University  College  Sta,on,  TX  77843  [email protected]  

 

Adapted  from  Gene  Hunt  Session  1  and  2  -­‐  hYp://paleobiology.si.edu/staff/individuals/hunt.cfm  

Page 2: The$DataFrame,$Type$Conversion,$ Graphing$people.tamu.edu/~alawing/materials/ESSM689/DataFrame.pdf · ESSM689$Quan,tave$Methods$in$ Ecology,$Evolu,on$and$Biogeography$ Ecosystem$Science$and$Management|$

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Lists  

•  Used  to  combine  different  types  of  informa,on  into  a  single  object    

w  <-­‐  list(“yes”,  32,  TRUE)  w[2]    32      #  just  like  a  vector    

•  List  elements  can  have  names      me  <-­‐  list(name=“Michelle”,  id=40172)    me$name      “Michelle”    me[1]        “Michelle”    

Page 3: The$DataFrame,$Type$Conversion,$ Graphing$people.tamu.edu/~alawing/materials/ESSM689/DataFrame.pdf · ESSM689$Quan,tave$Methods$in$ Ecology,$Evolu,on$and$Biogeography$ Ecosystem$Science$and$Management|$

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Dataframes  

•  Rectangular  table  of  informa,on,  not  necessarily  of  the  same  type  (numbers,  text,  etc).    

•  Usually,  this  is  the  form  your  data  will  be  in  when  you  import  it  

Page 4: The$DataFrame,$Type$Conversion,$ Graphing$people.tamu.edu/~alawing/materials/ESSM689/DataFrame.pdf · ESSM689$Quan,tave$Methods$in$ Ecology,$Evolu,on$and$Biogeography$ Ecosystem$Science$and$Management|$

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Dataframes  

•  Rows  and  columns  can  be  accessed  like  a  matrix        abund[1,  3]    10      abund[2,  ]    #  all  of  2nd  row    

 •  Columns  can  be  accessed  by  name  (like  a  list)    

   abund$taxon2  #  all  of  2nd  column    

Page 5: The$DataFrame,$Type$Conversion,$ Graphing$people.tamu.edu/~alawing/materials/ESSM689/DataFrame.pdf · ESSM689$Quan,tave$Methods$in$ Ecology,$Evolu,on$and$Biogeography$ Ecosystem$Science$and$Management|$

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Dataframes  

•  Use  aYach()  to  access  variable  names  directly      aYach(abund)        taxon2          #  all  of  2nd  column        detach(abund)    #  undoes  the  aYach()    

Page 6: The$DataFrame,$Type$Conversion,$ Graphing$people.tamu.edu/~alawing/materials/ESSM689/DataFrame.pdf · ESSM689$Quan,tave$Methods$in$ Ecology,$Evolu,on$and$Biogeography$ Ecosystem$Science$and$Management|$

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Datasets  in  R  

•  Some  datasets  are  built-­‐in  to  R  for  purposes  of  illustra,on.  

•  They  can  be  accessed  using  the  data()  func,on.  

Page 7: The$DataFrame,$Type$Conversion,$ Graphing$people.tamu.edu/~alawing/materials/ESSM689/DataFrame.pdf · ESSM689$Quan,tave$Methods$in$ Ecology,$Evolu,on$and$Biogeography$ Ecosystem$Science$and$Management|$

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Exercise  on  Dataframes    

1.  Make  the  dataset  iris  available  by  typing  data(iris).  Take  a  look  a  the  data  by  typing  iris.  Note  that  row  names  are  index  numbers  and  the  columns  are  aYributes  of  the  flower  (a  descrip,on  of  what  the  variables  mean  can  be  found  by  typing  ?iris).  

2.  How  many  rows  and  columns  are  there  in  this  dataset?  

3.  Use  the  func,ons  mean()  and  median()  to  calculate  the  mean  and  median  of  sepal  length  and  petal  width.  

4.  Calculate  the  sepal  length  in  millimeters,  and  save  that  to  a  new  variable  called  sl.ml  

Page 8: The$DataFrame,$Type$Conversion,$ Graphing$people.tamu.edu/~alawing/materials/ESSM689/DataFrame.pdf · ESSM689$Quan,tave$Methods$in$ Ecology,$Evolu,on$and$Biogeography$ Ecosystem$Science$and$Management|$

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Tes,ng  Rela,onships  

x  <-­‐  4  x  >  10            FALSE  x  <-­‐  c(4,  8,  30,  52)  x  >  10            FALSE  FALSE  TRUE  TRUE  x  >  10  &  x  <  50      FALSE  FALSE  TRUE  FALSE  

Page 9: The$DataFrame,$Type$Conversion,$ Graphing$people.tamu.edu/~alawing/materials/ESSM689/DataFrame.pdf · ESSM689$Quan,tave$Methods$in$ Ecology,$Evolu,on$and$Biogeography$ Ecosystem$Science$and$Management|$

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Subsemng  vectors  

 x  <-­‐  c(4,8,30,52)  

1.  Choose  by  their  indices      x[c(3,4)]      30    52  

2.  Using  logical  (T/F)  vectors      x[c(FALSE,FALSE,TRUE,TRUE)]    30    52      x  >  10              FALSE  FALSE  TRUE  TRUE      x[x  >  10]                  30    52  

Page 10: The$DataFrame,$Type$Conversion,$ Graphing$people.tamu.edu/~alawing/materials/ESSM689/DataFrame.pdf · ESSM689$Quan,tave$Methods$in$ Ecology,$Evolu,on$and$Biogeography$ Ecosystem$Science$and$Management|$

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Exercise:  Subsemng  

1.  Con,nuing  with  the  iris  dataset,  compute  separately  the  mean  sepal  length  of  each  species  (e.g.,  Iris  setosa  (==“setosa”).  

2.  Extract  out  a  vector  of  sepal  width  values  that  are  from  I.  setosa  and  I.  versicolor.  

Page 11: The$DataFrame,$Type$Conversion,$ Graphing$people.tamu.edu/~alawing/materials/ESSM689/DataFrame.pdf · ESSM689$Quan,tave$Methods$in$ Ecology,$Evolu,on$and$Biogeography$ Ecosystem$Science$and$Management|$

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Graphing  

•   Powerful  graphing  capabili,es  

•  Can  be  saved  as  vector  graphics  (PDF,  postscript)  

•  Can  add  to  a  plot,  but  can’t  edit  what’s  already  there  (not  clickable)  

Page 12: The$DataFrame,$Type$Conversion,$ Graphing$people.tamu.edu/~alawing/materials/ESSM689/DataFrame.pdf · ESSM689$Quan,tave$Methods$in$ Ecology,$Evolu,on$and$Biogeography$ Ecosystem$Science$and$Management|$

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Making  Graphs  

Generate  some  fake  data    x  <-­‐  rnorm(n=20,mean=0,sd=1)  #  random  normal  numbers                      #  same  as  rnorm(20,0,1)                      #  same  as  rnorm(20)    y  <-­‐  rnorm(n=20,mean=100,sd=10)  

 Try  Some  Plots…    

   plot(x)            plot(x,  y)      plot(x,  type  =  “l”)      plot(x,  y,  pch  =  2)      plot(x,  type  =  “b”)      plot(x,  y,  pch  =  3,  col  =  “red”)  

Page 13: The$DataFrame,$Type$Conversion,$ Graphing$people.tamu.edu/~alawing/materials/ESSM689/DataFrame.pdf · ESSM689$Quan,tave$Methods$in$ Ecology,$Evolu,on$and$Biogeography$ Ecosystem$Science$and$Management|$

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Page 14: The$DataFrame,$Type$Conversion,$ Graphing$people.tamu.edu/~alawing/materials/ESSM689/DataFrame.pdf · ESSM689$Quan,tave$Methods$in$ Ecology,$Evolu,on$and$Biogeography$ Ecosystem$Science$and$Management|$

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Page 15: The$DataFrame,$Type$Conversion,$ Graphing$people.tamu.edu/~alawing/materials/ESSM689/DataFrame.pdf · ESSM689$Quan,tave$Methods$in$ Ecology,$Evolu,on$and$Biogeography$ Ecosystem$Science$and$Management|$

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Argument  Informa,on