ma-d
ma-d

Reputation: 41

tapply with an ordered factor

When a factor level is missing you can use table in the following way:

marks <- c(1,5,3,4,5,6)
table(ordered(marks,levels=1:6))

which will return a table with level "2" listed with zero frequency.

If there were a set of "scores" associated with the "marks" and there were no missing levels (here 2), tapply could be used to generate the sum of scores for each level.

tapply(scores,marks,sum)

Can tapply be adapted to the 'missing' factor levels case? Or is there a better way?

Upvotes: 1

Views: 452

Answers (1)

agstudy
agstudy

Reputation: 121608

The idea here is to emulate table function behavior.

First , I generate a score vector , scores <- sample(1:6)

then in 2 steps:

  1. tapply to get scores with NA on missng values. Here I use sum function like table function but we can use any custom function ( max, min,..)

     res <- tapply( scores , ordered(marks,levels=1:6),function(x) {sum(x)} )
    
  2. Then just replace missing values

     res[is.na(res)] <- 0
    

Upvotes: 1

Related Questions