Why not do this in R? It seems like the code would be quite a lot simple, and it's unlikely to be a performance bottleneck.