Sort values without repeating them, in R

Asked

Viewed 29 times

0

I created a new column in a Tibble, to use as a second column identifier. However, there are repeated values, and I would like these repeated values to have the same identifier.

prec_med_novo <- mutate(prec_med, COD_SUBS = as.integer(length(25991)))

preco_med <- prec_med_novo[, c(37, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
                               24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36)]
preco_med

Column 37 (i.e., the first, has the values I wish to change, and column 1, the values to which they will be bound.

  • What are the names of the 1st and 2nd columns?

  • length(25991) is class "integer", doesn’t need as.integer. And the value is always 1.

  • cod_subs, substance, respectively

1 answer

1


Here’s a solution with the package dplyr

Assign to the identifier variable the numbers of the match between substancia and unique(substancia).

library(dplyr)

prec_med %>% 
  mutate(cod_subs = match(substancia, unique(substancia)))
  • In both cases, he assigned a value to the substance and, if this substance repeated itself, put the next value. But he still attributed the same value to different substances. In this case, the substance A must always have the value 1, regardless of how many times it is repeated; the substance B has the value 2, so on

  • @Leonardogonçalvesdafonseca Thanks for the additional explanation. See now after the edition.

  • It worked perfectly! Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.