Currently I have two vectors in R, one is a set of longitude levels (length m) and the other is a set of latitude levels (length n). I'm supposed to combine them into a matrix that's of size n x m. I understand that I can use something such as rbind()
or cbind()
to combine them into columns, but I need a matrix where the coordinates create some sort of n x m grid that represents properly a map with longitude and latitude.
Asked
Active
Viewed 416 times
-1

Karolis Koncevičius
- 9,417
- 9
- 56
- 89

yqz09
- 15
- 5
-
What would you like to fill the matrix with? – missuse Apr 29 '18 at 16:20
-
The idea is to generate a sort of heat map utilizing the image() function, and I'm supposed to begin with this matrix. There seems to be two types of matrices that I need to create. One is where the cells are filled with the number of houses in each cell (in the context of coordinates the number of homes in a longitude, latitude area). The other is a matrix where the cells are filled with the average home price. I was told that average home can be done with aggregate(), but primarily I'm trying to figure how to best create this matrix. – yqz09 Apr 29 '18 at 17:24
-
1Use the `outer` function, e.g. `outer(long, lat, fun)` but you really do need to say how to get from (long, lat) to the value in the matrix and write `fun` to do that. – user2554330 Apr 29 '18 at 20:47
1 Answers
0
I was just about to write the same as @user2554330 in the comments above. Here an example:
m <- 4
n <- 3
longitude <- runif(m)
latitude <- runif(n)
outer(longitude, latitude, function(x, y) paste(x, ":", y))
The third argument for outer
is a function of (at least) two arguments. It has to be vectorized on these two arguments and has to return a vector of equal length.
However, if you are looking for a way to store a grid of points together with some associated value, then expand.grid
is probably better suited. This does not give you a nxm matrix but a dataframe with nxm rows, each of them representing an entry from the matrix. You can then add values to this dataframe, preferably using a vectorized function:
m <- 4
n <- 3
longitude <- sort(runif(m, 35, 45))
latitude <- sort(runif(n, 35, 45))
points <- expand.grid(long = longitude, lat = latitude)
points$dist <- geosphere::distHaversine(points, c(40,40))
points
#> long lat dist
#> 1 35.47003 36.08055 589824.8
#> 2 38.80128 36.08055 448775.9
#> 3 42.37399 36.08055 483359.7
#> 4 44.59732 36.08055 593811.2
#> 5 35.47003 39.27063 396699.9
#> 6 38.80128 39.27063 130967.4
#> 7 42.37399 39.27063 219108.9
#> 8 44.59732 39.27063 402352.0
#> 9 35.47003 42.59421 476254.2
#> 10 38.80128 42.59421 305683.5
#> 11 42.37399 42.59421 350418.6
#> 12 44.59732 42.59421 480743.1

Ralf Stubner
- 26,263
- 3
- 40
- 75
-
I feel kind of silly asking right after you answered, but what sort of method would be reasonable to fill the matrix? Would it involve some sort of mapply() function? – yqz09 Apr 29 '18 at 21:31
-
With `outer` you have to use a vectorized function. See the updated answer for an alternative approach. – Ralf Stubner Apr 30 '18 at 08:42
-
I'm trying to get the price average included into a matrix. I was able to create a working heatmap using a matrix created from table(long,lat), and this gives out the correct frequency of homes per cell which is what I was looking for. However, this time when I create a new variable in the data which is the average price according to the factor level cell, I'm unable to create a related matrix for this data. The original working plot which utilized table() seems to be based on showing the frequency of homes according to each cell. However this time frequency won't explain the average price. – yqz09 May 01 '18 at 22:47
-
@yqz09 You will have to post some example code for what is working and what you tried but did not work. The used data can be a random data set. – Ralf Stubner May 02 '18 at 09:47
-
I think I figured things out differently, https://stackoverflow.com/questions/9617348/reshape-three-column-data-frame-to-matrix-long-to-wide-format has a good way to use daply() – yqz09 May 04 '18 at 12:50