Such switches can be classified by two reasons. As far as I see it, the nobles typically speak in blank verse. It is formal way to communicate for them, for instance, the way the king, the queen speak to Hamlet, Polonius or Laertes, etc. whereas the characters who are not the social group that has lower status do not speak in verse but talk. For instance, the way the gravediggers speak to each other. However, we can observe that Hamlet speak both in blank-verse and in prose. Such switches can be explained by the fact who he speaks to, what he wants to express and what role-playing he does. Hamlet speaks in prose with gravediggers, actors, Ophelia and Polonius, Rosencrantz and Guildenstern, which somewhat shows his insignificant attitude towards those people. In contrast, Hamlet's soliloquies or deep thoughts and observations are written in verse.