Explain R tapply description

前端 未结 2 859
借酒劲吻你
借酒劲吻你 2021-01-31 05:04

I understand what tapply() does in R. However, I cannot parse this description of it from the documentaion:


Apply a Function Over a \"Ragged\" Array

Description:

         


        
2条回答
  •  逝去的感伤
    2021-01-31 05:25

    @joran's great answer helped me understand it (so please vote for his - I would have added it as comment if it wasn't too long for that), but this may be of help to some:

    In quite a few languages, you have twodimensional arrays. Depending on the language, these arrays have fixed dimensions (i.e.: each row has the same number of columns), or some languages allow the number of items per row to differ. So instead of:

    A: 1  2  3
    B: 4  5  6
    C: 7  8  9
    

    You could get something like

    A: 1  3
    B: 4  5  6
    C: 8
    

    This is called a ragged array because, well, the right side of it looks ragged. In typical R-style, we might represent this as two vectors:

    values<-c(1,3,4,5,6,8)
    names<-c("A", "A", "B", "B", "B", "C")
    

    So tapply with these two vectors as the first parameters indeed allows us to apply this function to each 'row' of our ragged array.

提交回复
热议问题