Jim Holtman's solution generates a data frame that can be easily indexed by year: setwd("/r-help/2010-03-10") x <- read.csv("restored.csv", as.is=TRUE)
require(reshape) x.m <- melt(x, id=c('Year', 'LocationID', 'SpeciesCode'), measure='PlotFreq') x.binary <- cast(x.m, Year + LocationID ~ SpeciesCode, length) On Thu, Mar 11, 2010 at 10:55 AM, Lanna Jin <lanna...@gmail.com>wrote: >>>> >>>>> Hi Jim, >>>>> >>>>> Here's an example of what I mean by a binary matrix ( 1 = species >>>>> presence, 0 = species absence, cols = species, rows = location). This is >>>>> just a sample of data from the Year 1998 >>>>> >>>>> ACMI2 ALLIUM ALST AMAR2 AMCA6 AMPS ANCA8 ANCA9 ANCY ANEMONE >>>>> ANGE ANNE APCA ARLU ASCA11 ASER3 ASLA5 ASNO ASOV ASSY ASVE BOCU BREU CAREX >>>>> CASE13 >>>>> PIPE_15 0 0 0 0 0 0 0 0 0 >>>>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 >>>>> 0 0 >>>>> PIPE_16 0 0 0 0 0 0 0 0 0 >>>>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 >>>>> 0 0 >>>>> PIPE_17 0 0 0 0 0 0 0 0 0 >>>>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 >>>>> 0 0 >>>>> PIPE_7 0 0 0 0 0 0 0 0 0 >>>>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 >>>>> 0 0 >>>>> PIPE_8 0 0 0 0 0 0 0 0 0 >>>>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 >>>>> 0 0 >>>>> PIPE_9 0 0 0 0 0 0 0 0 0 >>>>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 >>>>> 0 0 >>>>> PIPE_11 1 0 0 0 1 0 1 0 1 >>>>> 0 0 0 1 1 0 1 1 0 1 1 0 1 0 >>>>> 1 0 >>>>> PIPE_12 1 1 0 0 0 0 1 0 0 >>>>> 0 1 0 1 1 0 0 0 0 0 1 0 0 0 >>>>> 1 0 >>>>> PIPE_13 0 0 0 0 1 0 1 0 0 >>>>> 0 1 0 0 0 0 1 0 0 0 1 0 0 0 >>>>> 1 0 >>>>> PIPE_14 1 0 0 0 1 1 1 0 1 >>>>> 0 0 0 0 1 0 0 1 0 0 1 1 1 0 >>>>> 1 0 >>>>> To give you a better idea of what the data looks like: >>>>> >>>>> > head(data) >>>>> Type Year Location Species >>>>> 66 Restored 1998 PIPE_7 ACMI2 >>>>> 67 Restored 1998 PIPE_7 AMAR2 >>>>> 68 Restored 1998 PIPE_7 AMCA6 >>>>> 69 Restored 1998 PIPE_7 ANCY >>>>> 70 Restored 1998 PIPE_7 ASVE >>>>> 71 Restored 1998 PIPE_7 BOCU >>>>> > unique(data$Year) >>>>> [1] 1998 1999 2001 2005 2006 2009 >>>>> > unique(data$Type) >>>>> [1] "Restored" "Native" >>>>> > length(unique(data$Species)) >>>>> [1] 107 >>>>> >>>>> I've managed to individually pull out each matrix for each type (also, >>>>> for some reason when I do as.matrix, it converts the 1's to -1's. Not sure >>>>> why, but I've corrected it...). >>>>> >>>>> dataT<-table(data$Location,data$Species,data$Year,data$Type) >>>>> >>>>> # For Natives: >>>>> N98<--as.matrix(dataT[,,1,"Native"]);N98[which(N98<0)]<-1 >>>>> N99<--as.matrix(dataT[,,2,"Native"]);N99[which(N99<0)]<-1 >>>>> etc... etc... >>>>> N09<--as.matrix(dataT[,,6,"Native"]);N09[which(N09<0)]<-1 >>>>> >>>>> # For Restored: >>>>> R98<--as.matrix(dataT[,,1,"Restored"]);R98[which(R98<0)]<-1 >>>>> R99<--as.matrix(dataT[,,2,"Restored"]);R99[which(R99<0)]<-1 >>>>> etc... etc... >>>>> R09<--as.matrix(dataT[,,6,"Restored"]);R09[which(R09<0)]<-1 >>>>> >>>>> Do you know of a way to efficiently do this in a function? For some >>>>> reason, I can't figure out how to generate unique dataframes/matrices for >>>>> each unique i in a for loop function... >>>>> >>>>> Thanks, >>>>> Lanna >>>>> >>>>> >>>>> >>>>>> On Wed, Mar 10, 2010 at 12:07 PM, Lanna Jin <lanna...@gmail.com >>>>>>>> > wrote: >>>>>>>> >>>>>>>>> Hi All, >>>>>>>>> >>>>>>>>> If given a dataframe (long form) with Year, Species, and Location, >>>>>>>>> How would I write a function that would create a unique matrix of >>>>>>>>> Species & >>>>>>>>> Location for each Year? >>>>>>>>> >>>>>>>>> What I've tried doing is the following: >>>>>>>>> >>>>>>>>> data #dataframe >>>>>>>>> dataT<-table(data$Species,data$Location,data$Year) #creates tables >>>>>>>>> of >>>>>>>>> Species vs Location for each Year >>>>>>>>> >>>>>>>>> But I'm encountering issues individually indexing the tables and >>>>>>>>> converting >>>>>>>>> them into matrices in one fell swoop. >>>>>>>>> >>>>>>>>> r1997<-as.matrix(tableT[,,"1997"]) #How I would do it individually; >>>>>>>>> but I >>>>>>>>> can I make matrices for each year as a function? >>>>>>>>> >>>>>>>>> Thanks in advance for your suggestions! >>>>>>>>> >>>>>>>>> -- >>>>>>>>> Lanna Jin >>>>>>>>> >>>>>>>>> lanna...@gmail.com >>>>>>>>> 510-898-8525 >>>>>>>>> >>>>>>>>> >>>>>>>> >>>>>>>> [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.