for loop - Creating empty matrix and filling in date matched values in R -


i'm quite new r , apologise in advance post not being in usual format (i tried using dput() got weird output , have no idea how upload datasets i'm sorry).

i have dataset 6 colums (site, startdate, enddate, photodate, species, indiv). example:

site    year    startdate   enddate photodate   species indiv m1_7    2012    19/07/2012  10/08/2012  20/07/2012  sylvicapra grimmia  1 m1_7    2012    19/07/2012  10/08/2012  23/07/2012  crocuta crocuta 1 m1_7    2012    19/07/2012  10/08/2012  23/07/2012  potamochoerus larvatus  1 m1_7    2012    19/07/2012  10/08/2012  25/07/2012  hystrix cristata    1 m1_7    2012    19/07/2012  10/08/2012  27/07/2012  potamochoerus larvatus  1 m1_7    2012    19/07/2012  10/08/2012  27/07/2012  sylvicapra grimmia  1 m1_7    2012    19/07/2012  10/08/2012  28/07/2012  hippotragus equinus     1 m1_7    2012    19/07/2012  10/08/2012  30/07/2012  crocuta crocuta 1 m1_7    2012    19/07/2012  10/08/2012  01/08/2012  equus q. boehmi 1 m1_7    2012    19/07/2012  10/08/2012  01/08/2012  crocuta crocuta 1 m1_7    2012    19/07/2012  10/08/2012  05/08/2012  potamochoerus larvatus  1 m1_7    2012    19/07/2012  10/08/2012  07/08/2012  hippotragus equinus     1 m1_9    2012    21/07/2012  11/08/2012  24/07/2012  pedetes capensis    1 m1_9    2012    21/07/2012  11/08/2012  24/07/2012  crocuta crocuta 2 m1_9    2012    21/07/2012  11/08/2012  24/07/2012  pedetes capensis    1 m1_9    2012    21/07/2012  11/08/2012  27/07/2012  pedetes capensis    1 m1_9    2012    21/07/2012  11/08/2012  01/08/2012  alcelaphus b. lichtensteinii    1 m1_9    2012    21/07/2012  11/08/2012  03/08/2012  pedetes capensis    1 m1_9    2012    21/07/2012  11/08/2012  04/08/2012  crocuta crocuta 1 m1_9    2012    21/07/2012  11/08/2012  06/08/2012  pedetes capensis    1 m1_9    2012    21/07/2012  11/08/2012  07/08/2012  pedetes capensis    1 m1_9    2012    21/07/2012  11/08/2012  08/08/2012  pedetes capensis    1 m1_11   2012    21/07/2012  11/08/2012  26/07/2012  mellivora capensis  1 m1_11   2012    21/07/2012  11/08/2012  03/08/2012  sylvicapra grimmia  1 m1_11   2012    21/07/2012  11/08/2012  07/08/2012  hystrix cristata    1 m1_11   2012    21/07/2012  11/08/2012  08/08/2012  potamochoerus larvatus  1 

i've been trying write loop creates 49 column matrix column 1 corresponds site, column 2 sequence of dates between "startdate" , "enddate" within site, column 3:49 species names. within cells under columns 3:49, fill them data derived summing count data (indiv), particular species, @ particular date.

so far have been able create empty matrix corresponding want, have been unable fill in data. code have used:

mlele2012<- read.delim("c:\\multiple regression\\mlele 2012 empty matrix creation.txt") africa <- read.delim("c:\\species accumulation curves\\complete species list.txt") specieslistx<-unique(africa) specieslistx<-t(specieslistx)   oldtemp<-null  temp <- rep(0, length(specieslistx ))  strptime(mlele2012$photodate, "%y-%m-%d") strptime(mlele2012$startdate, "%d/%m/%y") strptime(mlele2012$enddate, "%d/%m/%y")  #create empty dataframe dimensions: no. of sites x no. of dates in each  for(i in levels(mlele2012$site))    { ##for each site      sitetemp <- subset(mlele2012, site == i) ###subset of dataset , particular site i##      sitetemp$startdate<- as.date(sitetemp$startdate, "%d/%m/%y")     sitetemp$enddate<- as.date(sitetemp$enddate, "%d/%m/%y")      sitedatelist<-seq(as.date(sitetemp$startdate[1]), as.date(sitetemp$enddate[1]), "days")      empty<-matrix(0,length(sitedatelist),length(specieslistx))     sitedatelist1<-as.character(sitedatelist)     row.names(empty)<-(sitedatelist1)     colnames(empty)<-specieslistx      addsitecol<-matrix(0,length(sitedatelist),1)     extendempty<-cbind(addsitecol,empty)     extendempty[,1]<-i     oldtemp<-rbind(oldtemp, extendempty) }  write.csv(oldtemp, "mlele 2012 dry empty.csv") 

in addition, have been trying extract create matrix in same format/dimensions, without excess dates (i.e. dates in "photodate" column , not sequence between "startdate" , "enddate"). hoping somehow merge 2 matrices need. unfortunately code not work, although there doesn't seem error. here's second part of code:

for(i in mlele2012$site)    {        sitetemp <- subset(mlele2012, site == i) ###subset of dataset "allsites", particular site i##    for(j in sitetemp$photodate){       datetemp <- subset(sitetemp, photodate == j) ###subset of dataset "africaa", particular date i#       uniquespperdate <- unique(datetemp$species)###unique species within each date (row) i#       temp <- rep(0, length(specieslistx)) #create temporary vector of 0s same length species list###        for(a in uniquespperdate){          sptemp <- subset(datetemp , species == a) ###subset of dataset "sitetemp", particular sp j##          countdata<-sum(sptemp$indiv)          index <- pmatch(a, names(temp)) ###match unique species per date location on species list###          #there problem here, works when run single line not within loop          temp[index] <- countdata   ###for locations listed in "index", assign count data temporary vector###          names(temp)<- specieslistx       }    }            oldtemp <- rbind(oldtemp, temp) ### bind new temp file old temp file, i.e. update list loop runs### } 

any appreciated. kindly let me know if there details can provide make question clearer.

i can of way there on sample with:

> ftable(xtabs(indiv~site+year+species, data=dat) )            species boehmi capensis cristata crocuta equinus grimmia larvatus lichtensteinii site  year                                                                                  m1_11 2012              0        1        1       0       0       1        1              0 m1_7  2012              1        0        1       3       2       2        3              0 m1_9  2012              0        7        0       3       0       0        0              1 

i did input data using genus/species 2 columns because did not offer requested dput version.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -