[R-sig-Geo] Binding columns of pixel values

Ben Tupper btupper @end|ng |rom b|ge|ow@org
Thu Sep 9 19:57:19 CEST 2021


Hi,

In general you won't be happy with column (or row) binding if you are
starting out with vectors of varying lengths.  That's cbind and rbind
are what I call rectangle-centric.  They want to produce rectangular
data, like matrices and data.frames.  How do they make a rectangle
from 6 vectors of varying lengths?

You don't say but I suspect that you are using either the raster or
terra packages.  Are you extracting different points from each of the
six layers?  If that is the case you could make a single long table of
the extracted values...

eA <- data.frame(name = "A", value = extract(rasterA, xyA)
eB <- data.frame(name = "B", value = extract(rasterB, xyB)
 ...

r <- do.call(rbind, list(eA, eB, ...))


I'm not sure if this is valuable to you or not, but below is an
example of a multilayer SpatRaster.  It won't work if your 6 raster
layers have different sizes and extents.  A set of 3 points are
extracted (one row per point) from the three layers.

f <- system.file("ex/meuse.tif", package="terra")
r <- rast(f)
r <- c(r, r*10, r*100)
names(r) <- LETTERS[1:3]

xy <- cbind(179000, 330000)
xy <- rbind(xy-100, xy, xy+1000)
extract(r, xy)
#    A    B     C
#1 378 3780 37800
#2 251 2510 25100
#3 208 2080 20800

Ben


On Thu, Sep 9, 2021 at 12:52 PM Enoch Gyamfi Ampadu <egampadu using gmail.com> wrote:
>
> Dear List,
>
> I hope you are all well. Please I did an extraction of pixel values for 6
> land cover classes using *extract()*. Now I want to bind them into a
> single *data
> frame* but it is not working due to different lengths of columns. I have
> tried some *cbind* and at some point *rbind* but have still not been
> successful. I will be glad if anyone could assist me with an approach to
> use to bind the columns.
>
> Best regards,
>
> Enoch.
>
> --
> *Enoch Gyamfi - Ampadu, PhD*
>
> *FIDEP Foundation*
>
> *Spiritan University, *
>
> *S05, Ejisu, AR, Ghana*
>
> *Phone: +233 264308887 / +233 593630608*
>
> *email: egampadu using gmail.com <egampadu using gmail.com>*
>
>
> *skype: enoch.ampadu*
> *The highest evidence of nobility is self-control*.
>
> *A simple act of kindness creates an endless ripple*.
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



-- 
Ben Tupper (he/him)
Bigelow Laboratory for Ocean Science
East Boothbay, Maine
http://www.bigelow.org/
https://eco.bigelow.org



More information about the R-sig-Geo mailing list