在数据框上应用 group_by/do 时出错,tibble / assignment
Error when applying group_by/do over a data frame, tibble / assignment
我正在使用以下过程在分组数据帧上应用一个函数,dplyr:
Input2 <- data.frame(ens=rep(as.character(1:51), each=114),
dates_UTC= rep(as.character(seq(as.POSIXct("2013-01-01 07:00:00"), as.POSIXct("2013-01-06 00:00:00"), by="1 hour")), 51),
LE = sample(c(0,0,0,0,0,0,0,0,0.005,0.004,0.003,0.002,0.001), 114*51, replace=T),
ETPh = rep(0, 114*51),
ech = rep(1:114, 51),
NiveauResR = rep(c(32.1, rep(NA, 113)), each=51),
NiveauResS = rep(c(223, rep(NA, 113)), each=51),
HU1=rep(c(0.028, rep(NA, 113)), each=51),
HU2=rep(c(0, rep(NA, 113)), each=51),
HU3=rep(c(0, rep(NA, 113)), each=51),
HU4=rep(c(0, rep(NA, 113)), each=51),
HU5=rep(c(0, rep(NA, 113)), each=51),
HU6=rep(c(0, rep(NA, 113)), each=51))
Qmm_prev <- group_by(Input2, ens) %>%
dplyr::do(data.frame(dates_prev =.$dates_UTC, Q = test2(.))) %>%
unnest(cols=c())
有(从现实中简化)
test2 <- function(x){
Qmm_prev <- vector(length=nrow(x))
for (ech in 1:nrow(x))
{
if (ech < 114){
x[ech, 8:ncol(x)] <- c(0,0,0,0,0,0)
}
Qmm_prev[ech] <- 10
}
return(Qmm_prev)
}
我有以下错误:
Error: Assigned data `c(0, 0, 0, 0, 0, 0)` must be compatible with row subscript `ech`.
x 1 row must be assigned.
x Assigned data has 6 rows.
i Row updates require a list value. Do you need `list()` or `as.list()`?
Run `rlang::last_error()` to see where the error occurred.
这段代码在几个月前有效,如果我用“ens”上的循环替换 group_by/do 代码,它也有效。我相信 group_by/do 有语法错误,但我找不到它...我知道这来自这一行:
x[ech, 8:ncol(x)] <- c(0,0,0,0,0,0)
但是因为它在我用循环测试它时有效,所以我没有找到问题以及如何更正它...
有人知道吗?
谢谢
按照错误信息说的做:
将您的线路从:
更改为
x[ech, 8:ncol(x)] <- c(0,0,0,0,0,0)
至
x[ech, 8:ncol(x)] <- as.list(0,0,0,0,0,0)
并且代码按预期工作:
Qmm_prev <- group_by(Input2, ens) %>%
dplyr::do(data.frame(dates_prev =.$dates_UTC, Q = test2(.)))
但是,请注意 do
已被取代,可能有更好的写法 test2
是可能的。
我正在使用以下过程在分组数据帧上应用一个函数,dplyr:
Input2 <- data.frame(ens=rep(as.character(1:51), each=114),
dates_UTC= rep(as.character(seq(as.POSIXct("2013-01-01 07:00:00"), as.POSIXct("2013-01-06 00:00:00"), by="1 hour")), 51),
LE = sample(c(0,0,0,0,0,0,0,0,0.005,0.004,0.003,0.002,0.001), 114*51, replace=T),
ETPh = rep(0, 114*51),
ech = rep(1:114, 51),
NiveauResR = rep(c(32.1, rep(NA, 113)), each=51),
NiveauResS = rep(c(223, rep(NA, 113)), each=51),
HU1=rep(c(0.028, rep(NA, 113)), each=51),
HU2=rep(c(0, rep(NA, 113)), each=51),
HU3=rep(c(0, rep(NA, 113)), each=51),
HU4=rep(c(0, rep(NA, 113)), each=51),
HU5=rep(c(0, rep(NA, 113)), each=51),
HU6=rep(c(0, rep(NA, 113)), each=51))
Qmm_prev <- group_by(Input2, ens) %>%
dplyr::do(data.frame(dates_prev =.$dates_UTC, Q = test2(.))) %>%
unnest(cols=c())
有(从现实中简化)
test2 <- function(x){
Qmm_prev <- vector(length=nrow(x))
for (ech in 1:nrow(x))
{
if (ech < 114){
x[ech, 8:ncol(x)] <- c(0,0,0,0,0,0)
}
Qmm_prev[ech] <- 10
}
return(Qmm_prev)
}
我有以下错误:
Error: Assigned data `c(0, 0, 0, 0, 0, 0)` must be compatible with row subscript `ech`.
x 1 row must be assigned.
x Assigned data has 6 rows.
i Row updates require a list value. Do you need `list()` or `as.list()`?
Run `rlang::last_error()` to see where the error occurred.
这段代码在几个月前有效,如果我用“ens”上的循环替换 group_by/do 代码,它也有效。我相信 group_by/do 有语法错误,但我找不到它...我知道这来自这一行:
x[ech, 8:ncol(x)] <- c(0,0,0,0,0,0)
但是因为它在我用循环测试它时有效,所以我没有找到问题以及如何更正它...
有人知道吗?
谢谢
按照错误信息说的做:
将您的线路从:
更改为x[ech, 8:ncol(x)] <- c(0,0,0,0,0,0)
至
x[ech, 8:ncol(x)] <- as.list(0,0,0,0,0,0)
并且代码按预期工作:
Qmm_prev <- group_by(Input2, ens) %>%
dplyr::do(data.frame(dates_prev =.$dates_UTC, Q = test2(.)))
但是,请注意 do
已被取代,可能有更好的写法 test2
是可能的。