replacing NA with for loop vs dplyr

You need to include your data for it to be reproducible by others. That's why you're getting errors right away— tab doesn't exist.

This post might help: