unexpected output from aggregate

后端 未结 1 2455
广开言路
广开言路 2021-02-20 19:08

While experimenting with aggregate for another question here, I encountered a rather strange result. I\'m unable to figure out why and am wondering if what I\'m doi

相关标签:
1条回答
  • 2021-02-20 19:18

    The issue here is how aggregate.data.frame() determines the groups.

    In aggregate.data.frame() there is a loop which forms the grouping variable grp. In that loop, grp is altered/updated via:

    grp <- grp * nlevels(ind) + (as.integer(ind) - 1L)
    

    The problem with your example if that once by is converted to factors, and the loop has gone over all of these factors, in your example grp ends up being:

    Browse[2]> grp
    [1] Inf Inf Inf Inf
    

    Essentially the looping update pushed the values of grp to a number indistinguishable from Inf.

    Having done that, aggregate.data.frame() later does this

    y <- y[match(sort(unique(grp)), grp, 0L), , drop = FALSE]
    

    and this is where the earlier problem now manifests itself as

    dim(y[match(sort(unique(grp)), grp, 0L), , drop = FALSE])
    

    because

    match(sort(unique(grp)), grp, 0L)
    

    clearly returns just 1:

    > match(sort(unique(grp)), grp, 0L)
    [1] 1
    

    as there is only one unique value of grp.

    0 讨论(0)
提交回复
热议问题