'How to make data frame into raster object R

I have a data frame with one column of values. How do I make this into a raster image with the following parameters? I would also like to save it as a .tif raster image.

dimensions: (6,7,42) (nrow, ncol, ncell)

resolution (30,30) (x,y)

extent: (286695, 286905, 4620945, 4621125) (xmin, xmax, ymin, ymax)

crs: +proj=utm +zone=17 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0

values: 0, 65535 (min, max)

Thanks for your help!

library(raster)

#making dataframe
b1DF <- data.frame("b1" = c(18880, 19364, 20446, 21070, 23001, 22981, 23362,
 19538,18902, 19013, 18676, 20998, 23206, 24784, 21979, 20804, 20496, 19993,
 20830, 22396, 24246, 19877, 21002, 21771, 21478, 20827, 21267, 23036, 20103,
 21220, 22308, 21443, 20780, 21874, 23029, 19521, 20173, 20826, 21000, 21693,
 23388, 23935))


Solution 1:[1]

Pretty much like in most examples that come with the raster package.

b1DF <- data.frame(b1 = c(18880, 19364, 20446, 21070, 23001, 22981, 23362,
 19538,18902, 19013, 18676, 20998, 23206, 24784, 21979, 20804, 20496, 19993,
 20830, 22396, 24246, 19877, 21002, 21771, 21478, 20827, 21267, 23036, 20103,
 21220, 22308, 21443, 20780, 21874, 23029, 19521, 20173, 20826, 21000, 21693,
 23388, 23935))

library(raster)
r <- raster(nrow=6, ncol=7, ext=extent(286695, 286905, 4620945, 4621125), crs="+proj=utm +zone=17 +datum=WGS84 +units=m")
values(r) <- b1DF$b1

r
#class      : RasterLayer 
#dimensions : 6, 7, 42  (nrow, ncol, ncell)
#resolution : 30, 30  (x, y)
#extent     : 286695, 286905, 4620945, 4621125  (xmin, xmax, ymin, ymax)
#crs        : +proj=utm +zone=17 +datum=WGS84 +units=m +ellps=WGS84 +towgs84=0,0,0 
#source     : memory
#names      : layer 
#values     : 18676, 24784  (min, max)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Robert Hijmans