[Rd] Inconsistency between matrix and data.frame replacement matrix arg

Gabor Grothendieck
Sun, 23 Feb 2025 13:16:29 -0000

Using vector and array indexes with zeros works for extraction and
for replacement in the case of a vector, list and matrix but for
a data frame only extraction works.  All lines below give
the expected answer except the last line which gives the error message
shown.

  v <- 1:5
  L <- as.list(v)
  m <- cbind(a = 11:15, b = 21:25)
  DF <- as.data.frame(m)
  ix <- c(1, 0, 2)

  v[ix]
  L[ix]
  m[cbind(1:3, ix)]
  DF[cbind(1:3, ix)]

  v[ix] <- 99:100
  L[ix] <- 99:100
  m[ix, ]
  DF[ix, ]

  m[cbind(1:3, ix)] <- 99:100
  DF[cbind(1:3, ix)] <- 99:100
  ## Error in `[<-.data.frame`(`*tmp*`, cbind(1:3, ix), value = 99:100) :
  ##   'value' is the wrong length
  ## In addition: Warning message:
  ## In `[<-.data.frame`(`*tmp*`, cbind(1:3, ix), value = 99:100) :
  ##   number of items to replace is not a multiple of replacement length

--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com