[R] transpose dataset to PC-ORD?
Daniel Gruner
dsgruner at ucdavis.edu
Tue May 23 21:36:44 CEST 2006
Hello:
I need to take a species-sample matrix and transpose it to the format
used by PC-ORD for analysis. Unfortunately, the number of species is
very large (>5000), and so this operation cannot be performed simply
in an application like Excel, which has a 255 column limit. So, I
wrote relatively simple code in R that I hoped would do this
(appended below). But there are glitches.
The format needed for PC-ORD (where "NA" shows an empty cell):
NA,3,sites,NA
NA,3,species,NA
NA,Q,Q,Q
NA,sp1,sp2,sp3
site1,1,0,0
site2,0,1,2
site3,0,3,0
2 cells in first row indicate number of samples (rows), the second
column indicates number of species (columns), the third row indicates
variable type (Q = quantitative), and the fourth row shows column
headers (species names). So, one can create a transposable matrix in
a spreadsheet where 5000+ species are the rows:
NA,NA,NA,NA,site1,site2,site3
3,3,Q,sp1,1,0,0
sites,species,Q,sp2,0,1,3
NA,NA,Q,sp3,0,2,0
It is important that the data file written out is totally clean and
ready to go for PC-ORD, because I cannot open and edit it in a
spreadsheet. However, the code performs the transpose operation and
writes the file, but now the former row IDs are the first row in the
new file (NA,1,2,3), and the 4 leading spaces are "X, X.1, X.2,
X.3". I'd like to delete the first row and delete the first 4 values
of column1, without deleting the column.
NA,1,2,3
X,3,islands,NA
X.1,3,speciesNA
X.2,Q,Q,Q
X.3,sp1,sp2,sp3
site1,1,0,0
site2,0,1,2
site3,0,3,0
I have tried various tricks that I will not list/belabor here
(various col.names, row.names, header, Extract, etc commands). Any
further hints on code that will either stop R from adding these, or
strip them at the end?
(PS, yes, I can learn how to my multivariate analyses in R and skip
PC-ORD, but I am time limited on this one, and it seems that this
code could be very useful in numerous ways)
Many thanks for the help,
Dan Gruner
(Windows XP, R vers2.2)
##transpose datasets to convert to PC-ORD format
data<-read.csv("data.csv", header=TRUE, as.is=T,
strip.white=T, na.strings="NA")
data<-as.matrix(data)
data.trans <- t(data)
write.csv(data.trans, file = "datatransp.csv",
quote = F, na = "")
*******************************
Daniel S. Gruner, Postdoctoral Scholar
Bodega Marine Lab, University of California -- Davis
PO Box 247, 2099 Westside Rd
Bodega Bay, CA 94923-0247
(o) 707.875.2022 (f) 707.875.2009 (m) 707.338.5722
email: dsgruner_at_ucdavis.edu
http://www.bml.ucdavis.edu/facresearch/gruner.html
http://www.hawaii.edu/ant/
More information about the R-help
mailing list