每次通过for循环将向量添加到数据帧

时间:2020-08-27 19:56:05

标签: r for-loop append rbind

我的发现是10种不同年份的35种鸟类的丰度最高的前20个县。我创建了一个for循环,为我提供了一个县和每年的丰度数据框(两列,称为“ y”),我想将其放入10年中的每个年份的较大数据框中,因此每个物种列表中有200行数据。

我努力将一年(y)的数据帧添加到下一年。我尝试过rbindappend,但总是说我的替换行有误,或者在整个数据帧中重复了相同的20行

for (i in 1:35) {
    bbscounties[[i]]=as.data.frame(matrix(nrow=200, ncol=4))
    colnames(bbscounties[[i]])=c("Species", "Year", "CountyGEOID", "Abundance")
}
for (i in 1:35) {
    for(j in c(1992:1996,2013:2017)) {
    sub=subset(species[[i]], Year==j)
        for(k in 1:nrow(sub)) {
            pt1=st_sfc(st_point(c(sub[k,]$Longitude, sub[k,]$Latitude))) 
            cnty1=extract.county(pt1, counties)
            sub$County[k]=as.numeric(levels(cnty1$GEOID))[cnty1$GEOID]
            y=rbind(y,y)
            x=aggregate(sub$SpeciesTotal, by=list(County=sub$County), FUN=sum)
            y=arrange(x, desc(x))[1:20,]
            bbscounties[[i]][,1]=species[[i]]$AOU[1]
            bbscounties[[i]][,2]=sub$Year[1]
            bbscounties[[i]][,3]=y[,1]
            bbscounties[[i]][,4]=y[,2]
        }
    }
}
``

 head(species[[1]])
        X CountryNum StateNum Route RouteDataID RPID Year                AOU Count10 Count20 Count30 Count40 Count50 StopTotal SpeciesTotal Active Starttime
120 92256        840       14   103     6345387  101 2012 Aimophila_ruficeps       7       0       0       0       0         5            7      1       510
155 92291        840       14   103     6357807  101 2013 Aimophila_ruficeps       2       4       0       0       0         3            6      1       510
157 92293        840       14   103     6218217  101 1994 Aimophila_ruficeps       6       1       0       0       0         6            7      1       510
162 92298        840       14   103     6226712  101 1996 Aimophila_ruficeps      12       3       0       0       0        10           15      1       510
182 92318        840       14   103     6215329  101 1993 Aimophila_ruficeps       1       1       0       0       0         2            2      1       510
184 92320        840       14   103     6191824  101 1981 Aimophila_ruficeps       4       0       0       0       0         3            4      1       510
    Latitude Longitude Stratum BCR LT                 RT                  RTD County
120 34.56753 -118.5573      92  32 -  1 - Roadside route 1 - Random, 50 Stops    205
155 34.56753 -118.5573      92  32 -  1 - Roadside route 1 - Random, 50 Stops    205
157 34.56753 -118.5573      92  32 -  1 - Roadside route 1 - Random, 50 Stops    205
162 34.56753 -118.5573      92  32 -  1 - Roadside route 1 - Random, 50 Stops    205
182 34.56753 -118.5573      92  32 -  1 - Roadside route 1 - Random, 50 Stops    205
184 34.56753 -118.5573      92  32 -  1 - Roadside route 1 - Random, 50 Stops    205

nrow(species[[1]])
[1] 1992


extract.county=function(pt, counties){
     pt = st_sfc(pt)
   st_crs(pt)=st_crs(counties)
   pt=st_sf(pt)
   x=st_join(pt, counties)
   }



 bbscounties[[1]][1:50,]
              Species Year CountyGEOID Abundance
1  Aimophila_ruficeps 1992       48243        44
2  Aimophila_ruficeps 1992        4023        39
3  Aimophila_ruficeps 1992        4019        20
4  Aimophila_ruficeps 1992        4003        16
5  Aimophila_ruficeps 1992       35047        13
6  Aimophila_ruficeps 1992       48209        12
7  Aimophila_ruficeps 1992       48259        12
8  Aimophila_ruficeps 1992        4007        11
9  Aimophila_ruficeps 1992       48137        10
10 Aimophila_ruficeps 1992        6053         7
11 Aimophila_ruficeps 1992        6017         5
12 Aimophila_ruficeps 1992       48463         5
13 Aimophila_ruficeps 1992        6037         3
14 Aimophila_ruficeps 1992       48413         3
15 Aimophila_ruficeps 1992        6083         2
16 Aimophila_ruficeps 1992       35029         2
17 Aimophila_ruficeps 1992       40075         2
18 Aimophila_ruficeps 1992       48377         2
19 Aimophila_ruficeps 1992        4012         1
20 Aimophila_ruficeps 1992        4013         1
21 Aimophila_ruficeps 1992       48243        44
22 Aimophila_ruficeps 1992        4023        39
23 Aimophila_ruficeps 1992        4019        20
24 Aimophila_ruficeps 1992        4003        16
25 Aimophila_ruficeps 1992       35047        13
26 Aimophila_ruficeps 1992       48209        12
27 Aimophila_ruficeps 1992       48259        12
28 Aimophila_ruficeps 1992        4007        11


0 个答案:

没有答案