簡體   English   中英

將 function 應用於 dataframe r 的子集

[英]apply function to subsets of dataframe r

我正在嘗試通過兩個變量(“站點”和“年份”)對 dataframe 進行子集化,並將 function(dismo::biovars)應用於每個子集。 Biovars 需要每月輸入(12 個值)並每年輸出 19 個變量。 我想存儲每個子集的輸出並將它們組合起來。

示例數據:

data1<-data.frame(Meteostation=c(rep("OBERHOF",12),rep("SOELL",12)),
              Year=c(rep(1:12),rep(1:12)),
              tasmin=runif(24, min=-20, max=5),
              tasmax=runif(24, min=-1, max=30),
              pr=runif(24, min=0, max=300))

完整的數據集包含 900 個站點和 200 年。

我目前正在嘗試一個嵌套循環,我意識到這不是最有效的,而且我正在努力實現它 - 代碼如下:

sitesList <- as.character(unique(data1$Meteostation))
#yearsList<- unique(data1$Year)
bvList<-list()

for (i in c(1:length(unique(sitesList)))) {

   site<-filter(data1, Meteostation==sitesList[i])
   yearsList[i]<-unique(site$Year)

   for (j in c(1:length(yearsList))){

      timestep<-filter(site,Year==yearsList[j])
      tmin<-timestep$tasmin
      tmax<-timestep$tasmax
      pr<-timestep$pr

      bv<-biovars(pr,tmin,tmax)
      bvList[[j]]<- bv

}}

bv_all <- do.call(rbind, bvList)

我知道 go 有很多更好的方法來解決這個問題,並且一直在尋找應用的變體和 dplyr 解決方案,但我很難理解它。 非常感謝任何建議。

您可以使用 dplyr package,也許如下?

library(dplyr)
data1 %>% 
    group_by(Meteostation, Year) %>%
    do(data.frame(biovars(.$pr, .$tasmin, .$tasmax)))

使用byrbind結果。

library("dismo")
res <- do.call(rbind, by(data1, data1[c("Year", "Meteostation")], function(x) {
  cbind(x[c("Year", "Meteostation")], biovars(x$pr, x$tasmin, x$tasmax))
}))

生產

head(res[, 1:10])
#   Meteostation Year      bio1     bio2 bio3 bio4       bio5       bio6     bio7 bio8
# 1      OBERHOF    1 12.932403 18.59525  100   NA 22.2300284   3.634777 18.59525   NA
# 2      OBERHOF    2  5.620587  7.66064  100   NA  9.4509069   1.790267  7.66064   NA
# 3      OBERHOF    3  0.245540 12.88662  100   NA  6.6888506  -6.197771 12.88662   NA
# 4      OBERHOF    4  5.680438 45.33159  100   NA 28.3462326 -16.985357 45.33159   NA
# 5      OBERHOF    5 -6.971906 16.83037  100   NA  1.4432801 -15.387092 16.83037   NA
# 6      OBERHOF    6 -7.915709 14.63323  100   NA -0.5990945 -15.232324 14.63323   NA

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM