## Reclassify a vector based in another vector R

I have two vectors, or two columns of a data.frame and I want to the first column represent values let's say A and the second groups G, I want to change the groups based on the maximum values of A by G, then I want to change the values of G so that the group are ordered by max A. For example

df <-"A G 1.0 1 2.0 1 2.6 2 1.0 2 2.0 2 1.0 3 2.3 3" df <- read.table(textConnection(df), header=TRUE) aggregate(df$A, by=list(df$G), max)

then max by group are 2 2.6 2.3, and group 3 have to become 2, group 2 -> 3, and group 1 stays the same.

result should be

A G 1.0 1 2.0 1 2.6 3 1.0 3 2.0 3 1.0 2 2.3 2

It's easy to use agreggate or dplyr to obtain the max

require(dplyr) df %>% group_by(G) %>% summarise(Amax=max(A))

but I don't figure out how to change the groups.

You could use `dense_rank()`

on the max value by group. Although it's not clear what the result should be if max values are tied.

library(dplyr) df %>% mutate(G = dense_rank(ave(A, G, FUN = max))) A G 1 1.0 1 2 2.0 1 3 2.6 3 4 1.0 3 5 2.0 3 6 1.0 2 7 2.3 2

**reclass: Reclassify the values of a vector. in basille/basr: Basic, but ,** A character or numeric vector. from. A vector describing the values to change from, or a matrix of reclassification with two columns (from, to) Our sorting vector specifies that we want to order our first vector in the order “b”, “d”, “a”, “c”. Example: How to Sort First Vector According to Second. If we want to rearrange the order of our first vector based on the second vector, we can use a combination of the order and match functions. Have a look at the following R code:

Using `data.table`

and `match`

:

df=as.data.table(df) df[,maxg:=max(A),by=G][,G:=match(maxg,sort(unique(maxg)))][,maxg=NULL]

**Recode values,** Reclassify by layer is expecting you to use another vector layer's attributes For your use-case you need Reclassify by table or r.reclass from GRASS. So the idea is to classify a raster data based on a vector layer which In this lesson, you will learn how to reclassify a raster. When you reclassify a raster you create a new raster object / file that can be exported and shared with colleagues and / or opened in other tools such as QGIS. When you reclassify a raster you create a new raster. In that raster, each cell from the old raster is mapped to the new raster.

We could join `df`

with a grouped - ordered - renamed version of itself, and get the new group:

library(data.table) setDT(df) df[df[, max(A), by = G][order(-V1)][, newG := .I], on = "G"] A G V1 newG 1: 2.6 2 2.6 1 2: 1.0 2 2.6 1 3: 2.0 2 2.6 1 4: 1.0 3 2.3 2 5: 2.3 3 2.3 2 6: 1.0 1 2.0 3 7: 2.0 1 2.0 3

What is in there:

`df[, max(A), by = G]`

gives you a data.table of the max values of A in each group G.

`[order(-V1)]`

sorts the values of `max(A)`

(stored as V1) in descending order.

`[, newG := .I]`

assings to variable `newG`

the row number.

`X[Y, on = "Z"]`

is the syntax for table joins in data.table (Join tables X and Y on variable Z)

If you want to remove `V1`

, then it's achieved by appending to the end of the chained command: `[, V1 := NULL]`

.

**Reclassifying by layer in QGIS?,** Reclassification is done with matrix rcl , in the row order of the reclassify table. Thus (You can also supply a vector that can be coerced into a n*3 matrix (with In that case, the right argument is automatically set to NA Post a new example:. Vector is a basic data structure in R. It contains element of the same type. The data types can be logical, integer, double, character, complex or raw. A vector’s type can be checked with the typeof() function. Another important property of a vector is its length.

**reclassify function,** Numerical vector, but allowed using with numerical vector of dst and length(src)==length(dst) . Source set of values. sparse. Logical. If image has a lot of NA values Vectorization is one of the defining attributes of the R language. R wouldn’t be R if it didn’t have some kind of vectorized version of an if…else statement. The problem The priceCalculator() function still isn’t very economical to use. If you have 100 clients, you’ll have to calculate the price for every client separately. Check …

**reclass function,** This is also a convenient way to create masks based on vector maps. Cutter produces a new map that contains all the vector data from the data map that fall into the extent of the vector The module is used in a similar way as r. reclass. Re: test if vector contains elements of another vector (disregarding the position) %in% Here, i %in% j Hope this helps, Michael On Mon, Aug 22, 2011 at 11:51 AM, Martin Batholdy < [hidden email] >wrote:

**Open Source GIS: A GRASS GIS Approach,** To illustrate, let's set up a vector that has missing values. A <- c(3, 2, NA, 5, 3, We can re-code all missing values by another number (such as zero) as follows:. The easier way is to use GRASS (also using the QGIS/GRASS plugin) and the r.reclass module. You will need to create a reclassification rule file, where you can use the keyword "through" or the wildcard "*" to reclassify multiple values in one rule. share. Share a link to this answer.