0

I have a CSV (value, carbon, latitude, longitude) that I am trying to create a raster from. I am not able to change into raster. This code is not working.

CSV file sample:

enter image description here

class       : SpatialPointsDataFrame 
features    : 13135 
extent      : 35, 37, 73, 76  (xmin, xmax, ymin, ymax)
crs         : NA 
variables   : 3
names       : Carbon, Latitude, Longitude 
min values  :      1,       35,        73 
max values  :   5829,       37,        76 

R Script:

library(sp)  # vector data
library(raster)  # raster data
library(rgdal)  # input/output, projections
library(rgeos)  # geometry ops
library(spdep)  # spatial dependence

foresta<-carbonstock
head(carbonstock)

data<-data.frame(carbonstock$Longitude,carbonstock$Latitude,carbonstock$Carbon)
data<-data.frame(carbonstock)

# points from scratch

coords = cbind(carbonstock$Latitude, carbonstock$Longitude)
sp = SpatialPoints(coords)

# make spatial data frame

spdf = SpatialPointsDataFrame(coords, data)
spdf = SpatialPointsDataFrame(sp, data)

# promote data frame to spatial

coordinates(data) = cbind(carbonstock$Latitude, carbonstock$Longitude)
coordinates(data) = ~lon + lat

# back to data

as.data.frame(data)
plot(data,)

library(raster)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Ali Rawail
  • 21
  • 4
  • 1
    Hi! Welcome to Stackoverflow. Since you're new, it might be useful to keep in mind the following points: 1. Images are not helpful, we need data that we can execute. At minimum, please provide a reproducible sample [see this](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) 2. Please pay attention to the formatting of the post. A lot of your post appears to be code but is not formatted as such. 3. It is not good to include mostly code, please provide some more details (what is going wrong, what is the expected output and so on). – Aman Dec 28 '20 at 10:28
  • 1
    Please use the correct tags and format the. code. `Raster` refers to all non-vector formats. `carbon-copy` is the CC in emails. `How to convert CSV into raster` is meaningless. Do you mean how to *display* the data into a map and save it to an image? – Panagiotis Kanavos Dec 28 '20 at 10:28
  • See this [question](https://stackoverflow.com/questions/47752240/how-to-convert-data-frame-to-spatialpixelsdataframe-in-r). You can use `raster::rasterize` or `raster::interpolate`. – UseR10085 Dec 28 '20 at 12:08
  • What's the difference to your [previous question](https://stackoverflow.com/q/65455392/6574038)? – jay.sf Dec 28 '20 at 12:18

0 Answers0