1

行名として日付を、列名として TAG# を含むマトリックスがあります。マトリックスには、存在/不在を表す 0 と 1 が入力されます。例えば

           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

サイトの忠実度 (現在の日数の割合) を計算するための次のスクリプトがあります。

##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)

サイトの忠実度を週単位で計算したいと思います。7行ごとに組み合わせて、毎日の行列から0と1を単純に合計する週ごとの行列に単純に折りたたむのが最も簡単だと思います。次に、サイトの忠実度の同じスクリプトが週単位で計算します。問題は、私が初心者で、毎日のマトリックスを毎週のマトリックスに折りたたむ方法についての答えを見つけるのに苦労したことです。提案をありがとう。

4

2 に答える 2

2

このようなものが機能するはずです:

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")]

再現可能な例を提供すると、行名をより適切に保持できるようになります。優れたRの再現可能な例を作成するにはどうすればよいですか。

編集:また、上記のように正しい割り当てを使用することは非常に一般的で->はありません。

于 2013-01-18T21:56:21.097 に答える
0

R の関数は、s をその週cutにトリムします (詳細については、を参照してください)。その後、 を呼び出すだけで、必要な結果が得られます。オプションを取ることに注意してください。Date?cut.Dateaggregatecut.Datestart.on.monday

データ

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)

答え

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
于 2013-01-19T05:50:34.550 に答える