We have a column filled with unique values (e.g. S09.9, S72.30), in total there are 200 unique values. These values are connected seperately to a few rows (screenshot 1)enter image description here. We want to replace these values with numbers (1 ~ 200). Is there a way to do this with a loop or another function. If we want to replace these with the replace functions, that would mean we need to do this by hand and x 200.
Asked
Active
Viewed 56 times
-1
-
2Please provide a minimal reproducible example, preferably with `dput`.. – Mossa May 31 '22 at 10:08
-
1You can probably do something like `mydata$ICD10CodeHersen = as.numeric(as.factor(mydata$ICD10CodeHersen))`. But it is unclear exactly what you are trying to do - have a read of [this](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – SamR May 31 '22 at 10:19
1 Answers
0
Does this help?
You could make the code into an ordered or unordered factor
(See the help with ?factor
). Use levels
to see the unique values and count
to count them. And you can create a new numeric code for each unique factor level with as.numeric
.
library(tidyverse)
# Sample data
df <- tribble(
~code,
"S09.9",
"S06.00",
"S06.00",
"164",
"C79.5",
"S06.00",
"163.9",
"S09.9",
"S06.00",
"S06.00",
"R47.0",
"D43.2",
"S09.9",
"S06.00",
"S09.9",
"S06.00",
"G40.3",
"S09.9"
) |>
mutate(code = factor(code))
# Show the unique values (factor levels)
levels(df$code)
#> [1] "163.9" "164" "C79.5" "D43.2" "G40.3" "R47.0" "S06.00" "S09.9"
df |> mutate(code2 = as.numeric(code)) # Replace each level with a number
#> # A tibble: 18 × 2
#> code code2
#> <fct> <dbl>
#> 1 S09.9 8
#> 2 S06.00 7
#> 3 S06.00 7
#> 4 164 2
#> 5 C79.5 3
#> 6 S06.00 7
#> 7 163.9 1
#> 8 S09.9 8
#> 9 S06.00 7
#> 10 S06.00 7
#> 11 R47.0 6
#> 12 D43.2 4
#> 13 S09.9 8
#> 14 S06.00 7
#> 15 S09.9 8
#> 16 S06.00 7
#> 17 G40.3 5
#> 18 S09.9 8
# Count the occurrences
df |> count(code, sort = TRUE)
#> # A tibble: 8 × 2
#> code n
#> <fct> <int>
#> 1 S06.00 7
#> 2 S09.9 5
#> 3 163.9 1
#> 4 164 1
#> 5 C79.5 1
#> 6 D43.2 1
#> 7 G40.3 1
#> 8 R47.0 1
Created on 2022-05-31 by the reprex package (v2.0.1)

Carl
- 4,232
- 2
- 12
- 24