【发布时间】:2020-11-16 18:07:17
【问题描述】:
我正在尝试对一年半不规则间隔测量的树木生长值进行线性插值。
我想按树、块和基因型信息对木材体积组进行每日线性插值。但是,我的代码中有些地方不对。我尝试了参数“do”和“mutate”,但没有一个工作。有人可以帮我吗?
bio2 <- read.xlsx("Cres_biomassa.xlsx", h=T, sheetName = "Original")
str(bio2)
bio2$Block <- as.factor(bio2$Block)
bio2$Tree <- as.factor(bio2$Tree)
dput(bio2[1:10, ])
# structure(list(Date = structure(c(17537, 17593, 17628, 17656,
# 17695, 17730, 17761, 17782, 17817, 17836), class = "Date"), Block = # structure(c(1L,
# 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", "2", "3"
# ), class = "factor"), Gen = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
# 1L, 1L, 1L, 1L), .Label = c("G1", "G10"), class = "factor"),
# Tree = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
# ), .Label = c("1", "2"), class = "factor"), Volume = c(12.0152502828382,
# 121.168369070794, 324.280007440298, 522.317155691492, 684.262691983242,
# 742.921025749914, 775.35053835085, 804.747031488978, 996.719631625931,
# 1358.37974592578)), row.names = c(NA, 10L), class = "data.frame")
library(lubridate)
#Dates for daily interpolation:
Dates <- seq.Date(ymd("2018-01-06"), ymd("2019-04-06"), by = 1)
test1 <- bio2 %>%
group_by(Block, Gen, Tree) %>%
mutate(ApproxFun <- approxfun(x = bio2$Date, y = bio2$Volume)
LinearFit <- ApproxFun(Dates))
这个样本有两个基因型(Gen),两个树(Tree)和三个块(Blocks)。
【问题讨论】:
-
您能否通过发布一些示例数据使您的问题可重现?
dput()是最好的方法,因为它可以复制/粘贴并保留类和结构信息。例如,dput(bio2[1:10, ])表示前 10 行。选择一个合适的子集,比如 10 行,每行有 2 个组中的几个缺失值。 -
感谢您的评论。我已经满足你的要求了。
标签: r dplyr interpolation linear-programming