在 R 4.0 中替换 tibble 中的值

Replace values in tibble in R 4.0

我刚刚从 R 3.6.2 升级到 R 4.0.0,我用来替换 tibble 中的值的一些功能不再有效。我找不到我现在需要做什么。有谁知道 "new" 方法吗?

library(tidyverse)
v <- c(1, 2, 3)
w <- c(4, 4)
i <- 1

# Does not work anymore
df <- tibble(a = v, b = v, c = v)
df[i, 2:3] <- w

# This used to work with tibbles
df.old <- data.frame(a = v, b = v, c = v)
df.old[i, 2:3] <- w

这是我遇到的错误:

Error: Assigned data `w` must be compatible with row subscript `i`.
x 1 row must be assigned.
x Assigned data has 2 rows.
i Only vectors of size 1 are recycled.

谢谢,

在我的 R-devel 版本中,错误消息包括

ℹ Row updates require a list value. Do you need `list()` or `as.list()`?

所以这个版本中的规范方式可能是 df[i, 2:3] <- as.list(w),它有效:

library(tidyverse)
v <- c(1, 2, 3)
w <- c(4, 4)
i <- 1

df <- tibble(a = v, b = v, c = v)
df[i, 2:3] <- as.list(w)
df
#> # A tibble: 3 x 3
#>       a     b     c
#>   <dbl> <dbl> <dbl>
#> 1     1     4     4
#> 2     2     2     2
#> 3     3     3     3

reprex package (v0.3.0)

于 2020-04-26 创建