r语言 - 将平面文件转换为矩阵:基于两个唯一组的特定日期范围



我有一个大的平面文件,其中包含灰熊的标记-重新捕获数据。为了分析(在程序MARK中)这些数据需要转换成一个矩阵。数据集大约有10,000行长

数据集示例如下:

Date          TrapType    BearID   
01-01-2013    Corral       1
01-24-2013    Corral       1
02-14-2013    Tree         1
01-04-2013    Corral       2
01-08-2013    Corral       2
01-18-2013    Tree         2
03-01-2013    Tree         2
01-01-2013    Coral        3
03-03-2013    Tree         4

Date是网站被检查的日期陷阱类型是用来收集样本的陷阱类型,围栏只在1月1日至1月30日部署,树木在1月1日至3月15日部署Bear ID是个人标识符

我需要使用特定的日期范围创建一个捕获矩阵,但棘手的部分是日期范围对于两种陷阱类型(Corral和Tree)将不相同

我希望每10天在Corral站点进行一次捕获分组,每30天在Tree站点进行一次捕获分组

因此,上面数据的捕获矩阵,其中行是熊,列是捕获会话,看起来像:

(Corral Jan1-9) (Corral Jan10-19)  (Corral Jan20-30)  (Tree Jan1-30)  (Tree Feb1-30)  
(Tree Mar 1-30)
1    1   0   1   0   1   0                      
2    1   0   0   1   0   0
3    1   0   0   0   0   0
4    0   0   0   0   0   1

真正的文件不需要空格,我只是添加了它们,这样更容易查看。对如何做到这一点有什么想法吗?

我试着打破这两种陷阱类型,使用表格命令,但是当涉及到R时,我真的很陌生,我的命令工具包也很有限。

谢谢你的帮助!

这是另一个方法。首先,这是你的数据

dd<-data.frame(
    Date = c("01-01-2013", "01-24-2013", "02-14-2013", "01-04-2013", 
        "01-08-2013", "01-18-2013", "03-01-2013", "01-01-2013", "03-03-2013"), 
    TrapType = c("Corral", "Corral", "Tree", "Corral", 
        "Corral", "Tree", "Tree", "Corral", "Tree"),
    BearID = c(1, 1, 1, 2, 2, 2, 2, 3, 4)
)

首先,我确保日期列实际上是日期,并将BearID转换为因子

dd$Date <- as.Date(as.character(dd$Date), format="%m-%d-%Y")
dd$BearID <-factor(dd$BearID)

现在我将分别使用subset, tablecut.Date重塑两种陷阱类型,为每种陷阱类型使用不同的中断。注意,R可能对每个范围的长度有不同的想法。如果您想更具体,您可以将日期向量传递给cut()breaks=参数。

ctable<-with(subset(dd, TrapType=="Corral"), 
    table(BearID, cut(Date, breaks="10 days")))
colnames(ctable)<-paste("Corral", colnames(ctable))
ttable<-with(subset(dd, TrapType=="Tree"), 
    table(BearID, cut(Date, breaks="30 days")))
colnames(ttable)<-paste("Tree", colnames(ttable))

最后,我把这两个表放在一起得到最终的数据集

newdata<-cbind(ttable, ctable)

这个方法应该适用于更多的日期范围。

这是一种从长格式到宽格式的转换。但是,在使用某些特定的R工具(如dcastreshape)之前,您应该对数据进行预处理。可以这样做,例如:

## First I read your data   
xx <- read.table(text='Date          TrapType    BearID   
01-01-2013    Corral       1
01-24-2013    Corral       1
02-14-2013    Tree         1
01-04-2013    Corral       2
01-08-2013    Corral       2
01-18-2013    Tree         2
03-01-2013    Tree         2
01-01-2013    Coral        3
03-03-2013    Tree         4',header=TRUE)
## Coerce column Date to a valid date
xx$Date <- as.Date(xx$Date,format='%m-%d-%Y')
## Create a new column that aggregate corrals type and date
## this the tricky part!!
xx <- transform(xx,Type=
ifelse(TrapType=='Corral',{
  dd = as.numeric(format(xx$Date,'%d'))
  ifelse(dd<10,'Corral Jan1-9',
         ifelse(dd<20,'Corral Jan10-19','Corral Jan20-30'))
},{
  mm = as.numeric(format(xx$Date,'%m'))
  ifelse(mm<2,'Tree Jan1-31',
         ifelse(mm<3,'Tree Feb1-28','Tree Mar 1-31'))
}))
## put it in the wide format
library(reshape2)
dcast(BearID~Type,data=xx)
  BearID Corral Jan1-9 Corral Jan20-30 Tree Feb1-28 Tree Jan1-31 Tree Mar 1-31
1      1             1               1            1            0             0
2      2             2               0            0            1             1
3      3             0               0            0            1             0
4      4             0               0            0            0             1

最新更新