Reputation: 21
I am a bit of a R noob here hahaha. I need to create a polygon defined by a 2 column array with decimalised longitude (west of 0 = negative) in column 1 and decimalised latitude in column 2. The data is found in 4 columns in my dataframe (LonT,LonB,LatR and LatL), what I want is my matrix to resemble something like this:
[1] [2]
[1] LonT LatR
[2] LonB LatL
However, I am unsure of how to get this to work. This would then have to be run for each row in my dataframe.
Here's the header for my dataframe:
Ctry Year SR Lat Lon PType PNum Catch LonT LonB LatR LatL
1 DE 1998 29D8 50.25 -11.5 M 1 23.862 -11 -12 50.5 50.0
2 DE 1998 34D7 52.75 -12.5 M 1 42.499 -12 -13 53.0 52.5
3 DE 1998 34D8 52.75 -11.5 M 1 35.794 -11 -12 53.0 52.5
4 DE 1998 36D9 53.75 -10.5 M 1 23.862 -10 -11 54.0 53.5
5 DE 1998 43F7 57.25 7.5 M 1 0.017 8 7 57.5 57.0
6 DE 1998 45F4 58.25 4.5 M 1 0.058 5 4 58.5 58.0
Upvotes: 1
Views: 1215
Reputation: 887831
We can split
the subset of dataset by the sequence of rows and convert to a list
of matrix
es.
lst <- lapply(split(df1[9:12], seq_len(nrow(df1))), matrix, ncol=2)
lst[1:2]
#$`1`
# [,1] [,2]
#[1,] -11 50.5
#[2,] -12 50
#$`2`
# [,1] [,2]
#[1,] -12 53
#[2,] -13 52.5
df1 <- structure(list(Ctry = c("DE", "DE", "DE", "DE", "DE", "DE"),
Year = c(1998L, 1998L, 1998L, 1998L, 1998L, 1998L), SR = c("29D8",
"34D7", "34D8", "36D9", "43F7", "45F4"), Lat = c(50.25, 52.75,
52.75, 53.75, 57.25, 58.25), Lon = c(-11.5, -12.5, -11.5,
-10.5, 7.5, 4.5), PType = c("M", "M", "M", "M", "M", "M"),
PNum = c(1L, 1L, 1L, 1L, 1L, 1L), Catch = c(23.862, 42.499,
35.794, 23.862, 0.017, 0.058), LonT = c(-11L, -12L, -11L,
-10L, 8L, 5L), LonB = c(-12L, -13L, -12L, -11L, 7L, 4L),
LatR = c(50.5, 53, 53, 54, 57.5, 58.5), LatL = c(50, 52.5,
52.5, 53.5, 57, 58)), .Names = c("Ctry", "Year", "SR", "Lat",
"Lon", "PType", "PNum", "Catch", "LonT", "LonB", "LatR", "LatL"
), class = "data.frame", row.names = c("1", "2", "3", "4", "5",
"6"))
Upvotes: 1