'Find closest country based on lat long coordinates

I have many coastal coordinates. Now I want to find the country of the coordinates. Because some of the coordinates are (just) in the sea, I need some kind of nearest country code. The code I have used now gives many coordinates without output (NA) see below. Anyone who can help me?

enter image description here enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source