# Converting lat, long and values to raster doesn't project correctly

I have a some data like so:

``````x <-  c(59.06321, 59.22318, 59.38157, 59.53835, 59.69351, 59.84704, 59.99891, 60.14911, 60.29761, 60.44440, 60.58946, 60.73278, 60.87433, 61.01410, 61.15207, 61.28822, 61.42253, 61.55499, 61.68557, 61.81427, 61.94105, 62.06591, 62.18882, 55.01163, 55.20527, 55.39772, 55.58900, 55.77907, 55.96793, 56.15554, 56.34191)

y <- c(-151.2886, -150.8218, -150.3511, -149.8765, -149.3979, -148.9155, -148.4291, -147.9387, -147.4444, -146.9461, -146.4439, -145.9377, -145.4276, -144.9135, -144.3955, -143.8735, -143.3475, -142.8177, -142.2839, -141.7461, -141.2045, -140.6590, -140.1096, -161.7036, -161.3245, -160.9419, -160.5559, -160.1664, -159.7733, -159.3767, -158.9765)

z <- seq(1, length(y), 1)
``````

Where x, y and z represent latitude, longitude and values respectively.

The original data came in Lambert Conformal Conic Projection.

I am trying to turn these into a raster like so:

``````library (raster)

df <- data.frame(cbind(x, y, z))

ras <- rasterFromXYZ(df, crs = CRS("+proj=lcc +lat_1=20 +lat_2=60 +lat_0=40 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs"), digits = 0.2)

writeRaster(ras, out_path)
``````

These output data should be in Alaska as specified by the latitude and longitude, but they always come out in the Kansas/Nebraska border and don't quite look right.

The coordinate numbers are latitude and longitude - those numbers are lat-longs in Alaska, so you need to create the raster with the lat-long CRS. Also, you've got the X and Y mixed up so I'll resort the columns:

`````` > ras <- rasterFromXYZ(df[,c(2,1,3)],
crs =CRS("+init=epsg:4326"),digits=.2)
``````

Now you've got a raster with the right CRS, you can reproject it to another system:

``````> rasp <- projectRaster(ras,
crs="+proj=lcc +lat_1=20 +lat_2=60 +lat_0=40 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs")
``````

You'll see that the coordinates are now not lat-long:

``````> extent(rasp)
class       : Extent
xmin        : -3800660
xmax        : -2146260
ymin        : 2175211
ymax        : 3726211
``````