Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace values in tibble in R 4.0

I just upgraded to R 4.0.0 from R 3.6.2 and some functionality that I use to replace values in a tibble no longer works. I can't find what I need to do now. Does anyone know the "new" way?

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

This is the error that I get with the tibble:

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.

Thanks,

like image 582
zouth0 Avatar asked Apr 26 '20 17:04

zouth0


1 Answers

In my R-devel version, the error message includes

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

So the canonical way in this version is probably df[i, 2:3] <- as.list(w), which works:

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

Created on 2020-04-26 by the reprex package (v0.3.0)

like image 121
user12728748 Avatar answered Nov 09 '22 19:11

user12728748