% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/add_country.R
\name{add_country}
\alias{add_country}
\title{Add Country Information to a Dataset Based on Latitude/Longitude}
\usage{
add_country(dt, keep_geometry = FALSE)
}
\arguments{
\item{dt}{A \code{data.frame} or \code{data.table} containing columns
\code{lat} and \code{lon}.}

\item{keep_geometry}{Logical. If \code{TRUE}, the sf geometry column is kept.
Default is \code{FALSE}.}
}
\value{
A \code{data.table} with a new column \code{country}.
}
\description{
This function adds a new column \code{country} by performing a fast spatial
join between point coordinates (\code{lat}, \code{lon}) and world country
polygons from \pkg{rnaturalearth}. Geometry is handled via \pkg{sf}.
}
\details{
The input data must contain numeric columns named \code{lat} and \code{lon}
in WGS84 (EPSG:4326).  Rows with \code{NA} coordinates receive
\code{country = NA}.

After assigning countries, Germany is automatically split into
\code{"Germany-North"} and \code{"Germany-South"} using a latitude threshold:
\itemize{
\item lat >= 50 → \code{"Germany-North"}
\item lat  < 50 → \code{"Germany-South"}
}

If the input already contains a \code{country} column it is removed and
replaced by the spatially derived one.
}
\examples{
\donttest{
small <- data.table(
  lat = c(48.2, 51.0, 47.5),
  lon = c(16.3, 10.1, 8.5)
)

pep_with_country <- add_country(small)
pep_with_country
}

}
\author{
Matthias Templ
}
