Reputation: 119
I'm having a little problem that I can't resolve by myself.
I have a data frame containing some environmental information as well as the latitude and longitude for each sample stations. I want to divide my study area into 4 smaller areas (see the image below), whose information I want to include into a new column into my hole data frame. That is, my new column should be a factor with 4 levels, each of them corresponding to specific geolocation ranges based on each area limits.
As example, let say that my Area 1 is defined as being all sample stations located between longitudes (-10.4 to -10.6)
and latitudes (-37 to -36.2.)
. Thus, at the end I want to have a data frame like this:
Variable 1 Variable 2 Latitude Longitude Area
0.98 1.5 -10.2 -37.5 1
0.74 0.9 -10.1 -37.5 1
0.58 0.7 -11.7 -36.8 3
0.94 1.2 -11.9 -37.5 4
and so on. Can anyone help me to solve my problem?
Upvotes: 1
Views: 111
Reputation: 3557
If the intervals are solely determined by Latitude
, then you could use the following.
> Var1 <- runif(100)
> Var2 <- runif(100)
> Latitude <- runif(100, -13.0, -10.0)
> Longitude <- runif(100, -38.0, -36.0)
>
> dat <- data.frame(Var1, Var2, Latitude, Longitude)
>
> Area <- as.numeric(cut(dat$Latitude, breaks = c(-13.0, -12.0, -11.0, -10.0)))
>
> dat <- data.frame(dat, Area)
> dat
Var1 Var2 Latitude Longitude Area
1 0.655301728 0.14144585 -10.48851 -37.96152 3
2 0.107591891 0.72131348 -11.75188 -36.05489 2
3 0.639967329 0.98282855 -10.76784 -36.01176 3
4 0.295014765 0.31968068 -11.99204 -37.48354 2
5 0.373011497 0.21168608 -11.62501 -36.37215 2
6 0.802966559 0.69812115 -11.56078 -37.09359 2
7 0.013607591 0.47285961 -10.95633 -36.06122 3
8 0.309604142 0.14035926 -10.94194 -37.61688 3
9 0.066620024 0.15386860 -12.26123 -36.84730 1
10 0.066227174 0.70843535 -12.06751 -36.44342 1
11 0.551767865 0.60483061 -12.78234 -36.23624 1
12 0.770809846 0.80973319 -10.47946 -37.97104 3
13 0.909879222 0.04641956 -11.18606 -36.91503 2
Or you can use
Area <- as.numeric(cut(dat$Latitude, breaks = 4))
to have 4 intervals.
Upvotes: 1