Error in expand.grid

Hi friends,
I m new to R studio and I m facing the problem related to expand.grid. I m trying to create the grid for latitude and longitude. Please see the following codes.

tt<-read.csv(file.choose(),header = TRUE)
#print(tt)
head(tt)
library(marmap)
tt0<-getNOAA.bathy(lon1=63,lon2=78,lat1=7,lat2=25,resolution=4)
tt1<-expand.grid(lon,lat)

Error in expand.grid(lon, lat) : object 'lon' not found

the above line is error showed...

Rest of the codes are below
tt2<-get.depth(tt0,as.matrix(tt1[,1:2]),locator = FALSE)
head(tt0)
head(tt1)
head(tt2)
head(tt3)
tt3<-merge(tt2,tt,by=c("lon","lat"))
tt3$include<-0
tt3[tt3$depth>-200 & tt3$depth<0,]$include<-1

I m trying to extract data for 200m depth from whole file...
Please help me.
Thank you

The variables lon and lat are not defined anywhere, causing the error. Perhaps you mean

tt1 <- expand.grid(tt0$lon, tt0$lat)

That would produce a result like one of the following where I invented a small example data frame.

DF <- data.frame(lon = c(10, 10, 20), lat = c(5, 10, 10))
GRID <- expand.grid(DF$lon, DF$lat)
GRID
#>   Var1 Var2
#> 1   10    5
#> 2   10    5
#> 3   20    5
#> 4   10   10
#> 5   10   10
#> 6   20   10
#> 7   10   10
#> 8   10   10
#> 9   20   10

GRID2 <- expand.grid(unique(DF$lon), unique(DF$lat))
GRID2
#>   Var1 Var2
#> 1   10    5
#> 2   20    5
#> 3   10   10
#> 4   20   10

Created on 2019-11-07 by the reprex package (v0.3.0.9000)

1 Like

Dear sir , thanks a lot for help. i have used the codes given by you and it worked successfully. thanks you. :blush::smiley: