1

I would like to do some research about climbing. The dataset contain several columns with climbing grades, like these: 1,2,3,4a,4b,4c,5a,5b,5c,6a,6a+,6b,6b+,6c,6c+,7a,7a+....

I would like to use them as numbers. from 1 to 28 (from 1 to 9c). Tried to convert to factor, but plotting functions cannot deal with them as data for an axis. Also cannot use summarizing functions on them (like mean). Is it a way to assign values to characters?

Thank you!

Artem Sokolov
  • 13,196
  • 4
  • 43
  • 74
Kocz
  • 11
  • 1
  • Consider this answer: [Replace X-axis with own values](https://stackoverflow.com/a/5182416/6574038) – jay.sf Jun 21 '20 at 17:44

1 Answers1

0

Let's make up some example data. Notice that in this data frame, the grades are not in a particular order, some values are missing, and some are repeated:

df <- data.frame(grades = c('1','4a','3', '3', '3', '2','5a','4a','4b','4c'))
> df
   grades
1       1
2      4a
3       3
4       3
5       3
6       2
7      5a
8      4a
9      4b
10     4c

We can create a numeric ordering using the factor function, specifying all the unique levels in their intended order, with "ordered = T":

df$grades_factor = factor(df$grades, levels = c('1','2','3','4a','4b','4c','5a'), ordered = T)

Finally, we can simply convert this new column to numeric to reveal the underlying numerical values:

df$grades_num = as.numeric(df$grades_factor)

  grades grades_factor grades_num
1       1             1          1
2      4a            4a          4
3       3             3          3
4       3             3          3
5       3             3          3
6       2             2          2
7      5a            5a          7
8      4a            4a          4
9      4b            4b          5
10     4c            4c          6
jdobres
  • 11,339
  • 1
  • 17
  • 37
  • Thank you! :) That was the solution that I found. But if I want to make for example a scatterplot, I have to use the grades_num variable, which is fine for the graphical part, but on the x axis, there will be the numbers, not the labels :( – Kocz Jun 21 '20 at 16:09
  • There are many ways to substitute text labels for numbers on plot axes, depending on the plotting package used. But that's a separate issue. – jdobres Jun 21 '20 at 16:10