I have a vector that I have created from grouped data:
sitesMODE
FT KM KO LN LY
16.840000 32.230769 8.846154 237.000000 57.923077
I want to find the difference of each value to every value and ULTIMATELY report it in a single object or column so that I can plot it.
I will reproduce the data here(albeit in matrix form):
siteMODE <- matrix(c(16.84, 32.23, 8.84, 237.00, 57.92), 1, 5, byrow = TRUE)
colnames(siteMODE) <- c("FT", "KM", "KO", "LN", "LY")
I know that I can use:
diffMODELY <- abs(siteMODE - siteMODE[[5]])
to find the difference between the expressed column/element [[n]] so that:
diffMODELY
FT KM KO LN LY
41.08308 25.69231 49.07692 179.07692 0.00000
My question is now, how can I do this without having to create an object like diffMODELY for every column’s/element’s difference? AND how can I report the results into as a single object or column in a matrix?
Looking for something like this?
The
sapplypart computes the differences you want, but it gives you a matrix, since you want the resulting differences into a vector you may want to chose either the upper triangular matrix elements or the lower ones, that’s why I appliedupper.trifunction to select just the upper triangular elements and the final result is a vector.