s-u / fastmatch

Fast hashing functions and replacement of match()
18 stars 7 forks source link

ctapply(..., FUN=list) doesn't work since R 3.1.0 because list() longer duplicates #1

Open dselivanov opened 9 years ago

dselivanov commented 9 years ago

when FUN = list(), ctapply() works not as expected.

tapply(X = 1:10, INDEX = c(rep(1,5), rep(2,3), rep(3,2)), FUN = list)

$1 [1] 1 2 3 4 5

$2 [1] 6 7 8

$3 [1] 9 10

ctapply(X = 1:10, INDEX = c(rep(1,5), rep(2,3), rep(3,2)), FUN = list)

$1 $1[[1]] [1] 9 10

$2 $2[[1]] [1] 9 10

$3 $3[[1]] [1] 9 10

s-u commented 9 years ago

Thanks - this is an implication of reduced copying in R 3.1.x. ctapply re-uses value vectors if they fit (which is the case here since they are progressively shorter) and flags them with NAMED=2 which caused previous R versions to duplicate, but R 3.1.x never copies in list() anymore even with NAMED=2 so it assigns the same vector every time. Ironically, the fix is to always force a copy which will make this slower in most cases than older versions of R.

FWIW a quick fix for this particular case is to use alist instead of list.

dselivanov commented 9 years ago

Thanks for clarification, very helpful. I didn't imagine that this issue is related to so deep R internals :-)