Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 计算数据帧内变量的重复次数并计算出它';s比例发生_R - Fatal编程技术网

R 计算数据帧内变量的重复次数并计算出它';s比例发生

R 计算数据帧内变量的重复次数并计算出它';s比例发生,r,R,对R来说比较陌生,所以提前为自己的无知道歉 多年来,我在一个国家的多个地点处理多个(非常大的)观测数据集。我需要计算出在第x周注意到特定物种的地点占在第x周提交观察的地点总数的比例(基本上是存在/不存在数据)。我有一个数据集,提供每个物种观察的详细信息,还有一个数据集提供每周观察的总数量。所以我需要一些函数来计算在那一周内记录物种的地点的数量,然后除以在同一周内记录任何物种观察的地点的总数。 观察记录为一周(1-53)和一年(1995-2011) species.data示例(为便于粘贴,以cs

对R来说比较陌生,所以提前为自己的无知道歉

多年来,我在一个国家的多个地点处理多个(非常大的)观测数据集。我需要计算出在第x周注意到特定物种的地点占在第x周提交观察的地点总数的比例(基本上是存在/不存在数据)。我有一个数据集,提供每个物种观察的详细信息,还有一个数据集提供每周观察的总数量。所以我需要一些函数来计算在那一周内记录物种的地点的数量,然后除以在同一周内记录任何物种观察的地点的总数。 观察记录为一周(1-53)和一年(1995-2011)

species.data示例(为便于粘贴,以csv形式列出):

以及total.obs.data的示例:

YEAR, WEEKNO, TOTALOBS,
1995, 1, 100
1995, 2, 780
1995, 3, 100
1995, 4, 189
1995, 5, 382
1995, 6, 100
1995, 7, 899
1995, 8, 129

(因此,我不认为1995年第1周的比例为2/100,并且能够构建GLM或GAM)

让我尝试一下,同时注意上面评论中已经说明的问题的所有局限性

#Create the data frame with the total observations
tot.obs<-data.frame(year=rep(1995,10), weekno=1:10, obs=floor(runif(n=10,80,100)))
#Create the variable week-year
tot.obs$week.year<-paste(tot.obs$week,tot.obs$year,sep="-")

#Create the data frame species observations
species.data<-data.frame(site=factor(floor(runif(n=5,2000,3000))), week=c(1,1,2,4,7), year=rep(1995,5),observ=rep(1,5))
species.data$week.year<-paste(species.data$week,species.data$year,sep="-")
species.data$total.obs<-NA

#Match the total observations form the tot.obs data frame to the species data frame. You can probably do it much faster but here is a "quick and dirty way"

for (i in 1:dim(species.data)[1]){
  species.data$total.obs[i]<-tot.obs$obs[tot.obs$week.year==species.data$week.year[i]]  
}

#Calculates the percentage of the total observation that each center contributes
species.data$per.obs<-species.data$observ/ species.data$total.obs 

#For the presentation of the data, reshape is your friend
library(reshape)
species.data.melt<-melt(species.data,id.vars=c("site","week.year"), measure.vars="per.obs")

cast(species.data.melt,site~week.year, fun.aggregate=sum)


site     1-1995     2-1995     4-1995     7-1995
1 2436 0.00000000 0.00000000 0.01010101 0.00000000
2 2501 0.00000000 0.01123596 0.00000000 0.00000000
3 2590 0.00000000 0.00000000 0.00000000 0.01123596
4 2608 0.01030928 0.00000000 0.00000000 0.00000000
5 2942 0.01030928 0.00000000 0.00000000 0.00000000
#使用总观测值创建数据框

目前,数据过于简单,无法支持测试的复杂性。
xtabs
函数创建一个矩阵对象,该对象可除以该周的总数:

> xtblspec <-  xtabs( ~ SPECIES+ SITE_ID +WEEKNO + YEAR  , data=dat)     
> xtblspec
, , WEEKNO = 1, YEAR = 1995

         SITE_ID
SPECIES   1238 1286 1289 1538 1892 1894
  Attenb.    0    0    1    1    0    0

, , WEEKNO = 2, YEAR = 1995

         SITE_ID
SPECIES   1238 1286 1289 1538 1892 1894
  Attenb.    0    0    0    0    0    1

, , WEEKNO = 4, YEAR = 1995

         SITE_ID
SPECIES   1238 1286 1289 1538 1892 1894
  Attenb.    0    1    0    0    0    0

, , WEEKNO = 7, YEAR = 1995

         SITE_ID
SPECIES   1238 1286 1289 1538 1892 1894
  Attenb.    1    0    0    0    1    0
#-------------

weekobs <- totobs[ match( as.numeric(dimnames(xtblspec[ 1, ,  ,])$WEEKNO ) ,totobs$WEEKNO) ,
                  "TOTALOBS"]
#[1] 100 780 189 899
>xtblspec xtblspec
,WEEKNO=1,YEAR=1995
站点ID
物种1238128612891538189219894
注意。0    0    1    1    0    0
,WEEKNO=2,年份=1995
站点ID
物种1238128612891538189219894
注意。0    0    0    0    0    1
,WEEKNO=4,YEAR=1995
站点ID
物种1238128612891538189219894
注意。0    1    0    0    0    0
,WEEKNO=7,YEAR=1995
站点ID
物种1238128612891538189219894
注意。1    0    0    0    1    0
#-------------

weekobs你的问题并不难。您可以很容易地使用“重塑”和一些子集组合来实现这一点。但请提供可复制的样本数据集。例如,第二个数据集中的物种在哪里?如果它是一个大数据集,那么
数据表
包可能是您的朋友。正如@Tylerlinker所评论的,请定义“非常大”数据集的含义。有大量的数据集。
species.data.melt2<-melt(species.data,id.vars=c("week.year"), measure.vars="observ")
species.obs.total<-data.frame(cast(species.data.melt2,week.year~value, fun.aggregate=sum))
colnames(species.obs.total)[2]<-"aggregated.total"
species.obs.total$total<-NA

for (i in 1:dim(species.obs.total)[1]){
  species.obs.total$total[i]<-tot.obs$obs[tot.obs$week.year==species.obs.total$week.year[i]]  
}

species.obs.total$perc<-species.obs.total$aggregated.total/ species.obs.total$total
species.obs.total


  week.year aggregated.total total       perc
1    1-1995                2    97 0.02061856
2    2-1995                1    89 0.01123596
3    4-1995                1    99 0.01010101
4    7-1995                1    89 0.01123596
> xtblspec <-  xtabs( ~ SPECIES+ SITE_ID +WEEKNO + YEAR  , data=dat)     
> xtblspec
, , WEEKNO = 1, YEAR = 1995

         SITE_ID
SPECIES   1238 1286 1289 1538 1892 1894
  Attenb.    0    0    1    1    0    0

, , WEEKNO = 2, YEAR = 1995

         SITE_ID
SPECIES   1238 1286 1289 1538 1892 1894
  Attenb.    0    0    0    0    0    1

, , WEEKNO = 4, YEAR = 1995

         SITE_ID
SPECIES   1238 1286 1289 1538 1892 1894
  Attenb.    0    1    0    0    0    0

, , WEEKNO = 7, YEAR = 1995

         SITE_ID
SPECIES   1238 1286 1289 1538 1892 1894
  Attenb.    1    0    0    0    1    0
#-------------

weekobs <- totobs[ match( as.numeric(dimnames(xtblspec[ 1, ,  ,])$WEEKNO ) ,totobs$WEEKNO) ,
                  "TOTALOBS"]
#[1] 100 780 189 899
xtblspec <-  xtabs( ~ WEEKNO +SPECIES+ SITE_ID  + YEAR  , data=dat)
> xtblspec/weekobs
, , SITE_ID = 1238, YEAR = 1995

      SPECIES
WEEKNO     Attenb.
     1 0.000000000
     2 0.000000000
     4 0.000000000
     7 0.001112347

, , SITE_ID = 1286, YEAR = 1995

      SPECIES
WEEKNO     Attenb.
     1 0.000000000
     2 0.000000000
     4 0.005291005
     7 0.000000000

, , SITE_ID = 1289, YEAR = 1995

      SPECIES
WEEKNO     Attenb.
     1 0.010000000
     2 0.000000000
     4 0.000000000
     7 0.000000000

, , SITE_ID = 1538, YEAR = 1995

      SPECIES
WEEKNO     Attenb.
     1 0.010000000
     2 0.000000000
     4 0.000000000
     7 0.000000000

, , SITE_ID = 1892, YEAR = 1995

      SPECIES
WEEKNO     Attenb.
     1 0.000000000
     2 0.000000000
     4 0.000000000
     7 0.001112347

, , SITE_ID = 1894, YEAR = 1995

      SPECIES
WEEKNO     Attenb.
     1 0.000000000
     2 0.001282051
     4 0.000000000
     7 0.000000000