user1991659
user1991659

Reputation: 11

Combining matrix of daily rows into weekly rows

I have a matrix with dates as row names and TAG#'s as column names. The matrix is populated with 0's and 1's for presence/absence. eg

           29735 29736 29737 29738 29739 29740
2010-07-15     1     0     0     0     0     0
2010-07-16     1     1     0     0     0     0
2010-07-17     1     1     0     0     0     0
2010-07-18     1     1     0     0     0     0
2010-07-19     1     1     0     0     0     0
2010-07-20     1     1     0     0     0     0

I have the following script for calculating site fidelity (% days present):

##Presence/absence data setup
##import file
read.csv('pn.csv')->'pn'
##strip out desired columns
pn[,c(5,7:9)]->pn
##create table of dates and tags
table(pn$Date,pn$Tag)->T
##convert to a matrix
as.matrix(T)->U
##convert to binary for presence/absence
1*(U>2)->U

##insert missing rows
library(micEcon)
insertRow(U,395,0)->U
rownames(U)[395]<-'2011-08-16'
insertRow(U,253,0)->U
rownames(U)[253]<-'2011-03-26'
insertRow(U,250,0)->U
rownames(U)[250]<-'2011-03-22'
insertRow(U,250,0)->U
rownames(U)[250]<-'2011-03-21'

##for presence/absence
##define i(tag or column)
1->i
##define place to store results
cbind(colnames(U),rep(NA,length(colnames(U))))->sfresult
##loop instructions
for(i in 1:ncol(U)){
##identify first detection day
grep(1,U[,i])[1]->tagrow
##count total days since first detection
nrow(U)-tagrow+1->days
##count days present
length(grep(1,U[,i]))->present
##calculate site fidelity
present/days->sfresult[i,2]
}
##change class of results column
as.numeric(sfresult[,2])->sfresult[,2]
##histogram
bins<-c(0,.3,.6,1)
xlab<-c('Low','Med','High')
hist(as.numeric(sfresult[,2]), breaks=bins,xaxt='n', col=heat.colors(3), xlab='Percent      Days Present',ylab='Frequency (# of individuals)',main='Site Fidelity',freq=TRUE,labels=xlab)
axis(1,at=bins)

I'd like to calculate site fidelity on a weekly basis. I believe it would be easiest to simply collapse the matrix by combining every seven rows into a weekly matrix that simply sums the 0's and 1's from the daily matrix. Then the same script for site fidelity would calculate it on a weekly basis. Problem is I'm a newbie and I've had trouble finding an answer on how to collapse the daily matrix to a weekly matrix. Thanks for any suggestions.

Upvotes: 1

Views: 196

Answers (2)

Matthew Plourde
Matthew Plourde

Reputation: 44614

R's cut function will trim Dates to their week (see ?cut.Date for more details). After that, it's a simple call to aggregate to get the result you need. Note that cut.Date takes a start.on.monday option.

Data

sites <- read.table(text="29735 29736 29737 29738 29739 29740
  2010-07-15     1     0     0     0     0     0
  2010-07-16     1     1     0     0     0     0
  2010-07-17     1     1     0     0     0     0
  2010-07-18     1     1     0     0     0     0
  2010-07-19     1     1     0     0     0     0
  2010-07-20     1     1     0     0     0     0", 
  header=TRUE, check.names=FALSE, row.names=1)

Answer

weeks.factor <- cut(as.Date(row.names(sites)), 
                    breaks='weeks', start.on.monday=FALSE)
aggregate(sites, by=list(weeks.factor), FUN=function(col) sum(col)/length(col))

#      Group.1 29735     29736 29737 29738 29739 29740
# 1 2010-07-11     1 0.6666667     0     0     0     0
# 2 2010-07-18     1 1.0000000     0     0     0     0

Upvotes: 0

Michael
Michael

Reputation: 5898

Something like this should work:

x <- matrix(rbinom(1000,1,.2), nrow=50, ncol=20)
rownames(x) <- 1:50
colnames(x) <- paste0("id", 1:20)

require(data.table)
xdt <- as.data.table(x)

    ##assuming rows are sorted by date, that there are no missing days, and that the first row is the start of the week
    ###xdt[, week:=sort(rep(1:7, length.out=nrow(xdt)))] ##wrong

   xdt[, week:=rep(1:ceiling(nrow(xdt)/7), each=7)] ##fixed


xdt[, lapply(.SD,sum), by="week",.SDcols=setdiff(names(xdt),"week")]

I can help you better preserve rownames if you provide a reproducible example How to make a great R reproducible example?

Edit: Also, it's very atypical to use the right assignment -> as you do do above.

Upvotes: 2

Related Questions