9

我的采样数据分布在两个数据集上。loc描述地理位置,spe包含发现的物种。不幸的是,采样站由两个因素(cruisestation)描述,所以我需要为两个数据集构建唯一标识符

>loc
  cruise station     lon    lat
1    TY1      A1 53.8073 6.7836
2    TY1       3 53.7757 6.7009
3    AZ7      A1 53.7764 6.6758

>spe
  cruise station     species abundance
1    TY1      A1 Ensis ensis       100
2    TY1      A1    Magelona         5
3    TY1      A1    Nemertea        17
4    TY1       3    Magelona         8
5    TY1       3     Ophelia      1200
6    AZ7      A1     Ophelia       950
7    AZ7      A1 Ensis ensis        89
8    AZ7      A1        Spio         1

我需要的是添加一个唯一ID标识符

  cruise station     species abundance     ID
1    TY1      A1 Ensis ensis       100 STA0001
2    TY1      A1    Magelona         5 STA0001
3    TY1      A1    Nemertea        17 STA0001
4    TY1       3    Magelona         8 STA0002
5    TY1       3     Ophelia      1200 STA0002
6    AZ7      A1     Ophelia       950 STA0003
7    AZ7      A1 Ensis ensis        89 STA0003
8    AZ7      A1        Spio         1 STA0003

这是数据

loc<-data.frame(cruise=c("TY1","TY1","AZ7"),station=c("A1",3,"A1"),lon=c(53.8073, 53.7757, 53.7764),lat=c(6.7836, 6.7009, 6.6758))

spe<-data.frame(cruise=c(rep("TY1",5),rep("AZ7",3)),station=c(rep("A1",3),rep(3,2),rep("A1",3)),species=c("Ensis ensis", "Magelona", "Nemertea", "Magelona", "Ophelia", "Ophelia","Ensis ensis", "Spio"),abundance=c(100,5,17,8,1200,950,89,1))

然后,我构造了IDforloc

 loc$ID<-paste("STA",formatC(1:nrow(loc),width=4,format="d",flag="0"),sep="")

但我如何映射IDspe

我发现涉及两个嵌套循环的方式对于像我这样的程序程序员来说非常漂亮(如果嵌套循环可以被称为漂亮的话)。我很确定 R 中的两行代码会更高效、更快,但我想不通。我真的希望我的代码有更多的美感,这太不合时宜了。

4

3 回答 3

5

实际上,我认为这是merge在基本 R 中起作用的情况:

merge(spe, loc, all.x=TRUE)

  cruise station     species abundance     lon    lat
1    AZ7      A1     Ophelia       950 53.7764 6.6758
2    AZ7      A1 Ensis ensis        89 53.7764 6.6758
3    AZ7      A1        Spio         1 53.7764 6.6758
4    TY1       3    Magelona         8 53.7757 6.7009
5    TY1       3     Ophelia      1200 53.7757 6.7009
6    TY1      A1 Ensis ensis       100 53.8073 6.7836
7    TY1      A1    Magelona         5 53.8073 6.7836
8    TY1      A1    Nemertea        17 53.8073 6.7836

要查找唯一标识符,请使用unique()

unique(paste(loc$cruise, loc$station, sep="-"))
[1] "TY1-A1" "TY1-3"  "AZ7-A1"
于 2012-07-13T15:23:05.583 回答
3

您可以将因子与 结合起来interaction

如果您不关心 ID 列的标签,那么解决方案非常简单。

loc <- within(loc, id <- interaction(cruise, station))
spe <- within(spe, id <- interaction(cruise, station))
于 2012-07-13T15:27:38.067 回答
0

只是为了说明这会导致(可能感兴趣):

ID如前所述,添加唯一标识符loc

loc$ID<-paste("STA", formatC(1:nrow(loc), width=4, format="d", flag="0"), sep="")

正如 Andrie 所提议的 merge(spe, loc, all.x=TRUE)那样,根据需要组合 data.frames,消除loc其中可能没有对应项的所有元素spe(如果应该保留这些元素,请merge(spe, loc, all.x=TRUE, all.y=TRUE)改为使用。

我想要一个每个站所有物种丰度的表,它是通过以下方式实现并转换为数据框的

as.data.frame.matrix(xtabs(abundance ~ ID + species, merge(spe, loc, all.x=T)))
        Ensis ensis Magelona Nemertea Ophelia Spio
STA0001         100        5       17       0    0
STA0002           0        8        0    1200    0
STA0003          89        0        0     950    1

感谢安德烈和棉花先生

于 2012-07-16T09:27:10.950 回答