finalized R code for pb2