Difference between revisions of "HowTo/InputtingData"

From Comparative Phylogenetics in R
Jump to: navigation, search
 
 
(12 intermediate revisions by 8 users not shown)
Line 3: Line 3:
 
   library(ape)
 
   library(ape)
  
This loads the package ape and its required packages gee, lattice and ade4 into your R session.  
+
This loads the package ape and its required packages gee, nlme and lattice into your R session.
  
'''How do I input a phylogeny into R?'''
+
'''How do I input data corresponding to the tips of my phylogeny into R?'''
 +
 
 +
First, assemble your data into a tab delimited text file (this can be done, for example, in a simple text editor or as a "save as" option in MS Excel).  By default, the rows of the datafile correspond to the taxa at the tips of your phylogeny. The first column should represent the names of your taxa exactly as they appear in your phylogeny (see previous section). The second and any subsequent columns of the datafile should correspond to the variables in your dataset.  The first row of the datafile should contain the names of the variables in your dataset, noting that this row will have one fewer entry than the other rows.  In other words, do not supply a title for the column containing the taxon names.
 +
 
 +
Once the text file is properly formatted, the read.table command will import it as an object in R. For example, this will create the data table "MyData" from the file "MyDatafile.txt".
  
The package APE can read phylogenies in either Newick or NEXUS format using the read.tree and read.nexus commands, respectively.  
+
  MyData <- read.table ("MyDatafile.txt")
  
  MyTree <- read.tree("MyNewickTreefile.tre")
+
The procedure above should work on any white-space (space, tab or line-end) delimited text file. There are several other options for inputing data using read.table and its derivatives. For example, read.csv will read a comma-delimited text file.  Use the following command for more information on the nuances of read.table.
  MyTree <- read.nexus("MyNexusTreefile.nex")
 
  
Either of these commands will create an identical object in R containing your tree. Taxon names should appear without quotes. Note that the NEXUS format supports the use of a translation table for taxon names.
+
  help(read.table)
  
'''How do I export a phylogeny out of R?'''
+
To load the sample [[Media:geospiza.txt | Geospiza tip data file]] that is used in several of the worked examples, save the linked file to your working directory, then execute this command.
  
The package APE can export phylogenies in either Newick or Nexus format using the write.tree and write.nexus commands, respectivly. 
+
  geodata <- read.table ("geospiza.txt")
  
  write.tree(geospiza$geospiza.tree, file="geospiza.tre"
+
* [http://bodegaphylo.wikispot.org/III._Loading_Character_Data_into_R Also see Rich Glor's page on the Bodega Applied Phylogenetics Workshop wiki]
  write.nexus(geospiza$geospiza.tree, file="geospiza.nex"
 
  
'''How do I input data corresponding to the tips of my phylogeny into R?'''
 
  
First, assemble your data into a tab delimited text file (this can be done, for example, in a simple text editor or in MS Excel)
+
[[HowTo/Table of Contents| Back to the HowTo/Table of Contents]]
  
'''In what ways can I manipulate phylogenies in R?'''
+
[[Category:HowTo]]
 +
[[Category:R Help]]
 +
[[Category:Comparative Methods Help]]

Latest revision as of 20:22, 12 March 2009

The commands referenced below are all part of special phylogenetic packages in R, not the basic R install. Be sure that you have installed and loaded the packages containing the commands referenced below before continuing. For example:

  library(ape)

This loads the package ape and its required packages gee, nlme and lattice into your R session.

How do I input data corresponding to the tips of my phylogeny into R?

First, assemble your data into a tab delimited text file (this can be done, for example, in a simple text editor or as a "save as" option in MS Excel). By default, the rows of the datafile correspond to the taxa at the tips of your phylogeny. The first column should represent the names of your taxa exactly as they appear in your phylogeny (see previous section). The second and any subsequent columns of the datafile should correspond to the variables in your dataset. The first row of the datafile should contain the names of the variables in your dataset, noting that this row will have one fewer entry than the other rows. In other words, do not supply a title for the column containing the taxon names.

Once the text file is properly formatted, the read.table command will import it as an object in R. For example, this will create the data table "MyData" from the file "MyDatafile.txt".

  MyData <- read.table ("MyDatafile.txt")

The procedure above should work on any white-space (space, tab or line-end) delimited text file. There are several other options for inputing data using read.table and its derivatives. For example, read.csv will read a comma-delimited text file. Use the following command for more information on the nuances of read.table.

  help(read.table)

To load the sample Geospiza tip data file that is used in several of the worked examples, save the linked file to your working directory, then execute this command.

  geodata <- read.table ("geospiza.txt")


Back to the HowTo/Table of Contents