library(dplyr)
样本数据
df <- data.frame(year = rep(1981:1982, each = 365), doy = rep(1:365, times = 2),
tmean = sample(20:35, 730, replace = T), ref.doy = rep(c(60, 80), each = 365))
thermal.df <- data.frame(stage1 = 60, stage2 = 90, stage3 = 120)
我有一个功能
my.function <- function(tmean.ref, doy.ref, thermal.df){
tm <- tmean.ref[1:length(tmean.ref) >= doy.ref]
tbase <- 10; topt <- 31;tcri <- 40
fT <- ifelse(tm >= tbase & tm <= topt,(tm - tbase)/(topt - tbase), ifelse(topt <= tm & tm <= tcri,(tcri - tm)/(tcri - topt), 0))
thermal.units <- tbase + fT*(topt - tbase)
stage1 <- as.numeric(thermal.df[, "stage1"])
stage2 <- as.numeric(thermal.df[, "stage2"])
stage3 <- as.numeric(thermal.df[, "stage3"])
pheno <- list(stage1, stage2, stage3)
return(pheno)
}
将功能应用于df
df %>% dplyr::group_by(year) %>%
dplyr::summarise(x = paste(my.function(tmean.ref = tmean,
doy.ref = unique(ref.doy),
thermal.df = thermal.df),collapse = ","))
这将在单列中返回我的输出
# A tibble: 2 x 2
year x
<int> <chr>
1 1981 60,90,120
2 1982 60,90,120
而我希望我的输出为:
year stage1 stage2 stage3
1981 60 90 120
1982 60 90 120