4

I have the following dataset:

   Rate       WUE
1     1 11.071188
2     1 15.334987
3     1 11.074738
4     1 13.101628
5     1 17.516873
6     1 15.408335
7     1 15.064044
8     1 14.890052
9     1 14.552573
10    1 15.392169
11    1 14.041053
12    1 11.269646
13    2 17.320536
14    2  8.905063
15    2 16.376546
16    2 15.202152
17    2 13.262815
18    2 10.568496
19    2 15.904926
20    2 15.189488
21    2 16.714268
22    2 17.803819
23    2 16.217470
24    3  9.030102
25    3 17.508928
26    3 13.454914
27    3 16.403707
28    3  9.931559
29    3 20.487025
30    3 16.314383
31    3 17.023167
32    3 17.095327
33    3 12.090982
34    3 15.227999
35    3 12.996101
36    4 17.626385
37    4 16.791135
38    4 17.015173
39    4 21.266143
40    4 15.576164
41    4 10.649791
42    4 19.238123
43    4 17.586917
44    4 20.408033
45    4 14.832184
46    4 12.735369
47    4 17.657028

And would like to change the names of the "Rate" levels from 1-4 to actual descriptions. Is there a way to do this with code? I want to avoid doing it in excel. I used the search function on stackoverflow and was unable to find an answer that worked for a dataset like this. Thanks in advance for any help and please let me know if there's anything else I can add.

Tom
  • 377
  • 3
  • 13
  • This function might help: https://forcats.tidyverse.org/reference/fct_recode.html – MrFlick Nov 20 '20 at 18:51
  • See these SO posts: [1](https://stackoverflow.com/questions/5869539/confusion-between-factor-levels-and-factor-labels) and [2](https://stackoverflow.com/questions/52743871/inquiry-why-does-base-r-behave-this-way-with-factor/). – Rui Barradas Nov 20 '20 at 19:09

3 Answers3

3

Here is a solution using mutate from the dplyr package and fct_recode from the forcats package. Note that I have created sample data using tibble from the tibble package. Here is some additional information on creating a proper reproducible example.

mutate allows us to modify the existing Rate variable.

fct_recode allows you to change factor level names.

as.factor from base r is used to coerce the Rate variable to a factor.

If you're interested in reordering the factor levels, you should look into using fct_relevel from the forcats package.

Code:

library(tibble)
library(dplyr)
library(forcats)

df <- tibble(
  Rate = c(1, 1, 2, 2, 3, 3, 4, 4),
  WUE = c(10.1, 10.1, 11.2, 11.2, 12.3, 12.3, 13.4, 13.4)
)

df %>% 
  mutate(
    Rate = fct_recode(as.factor(Rate),
                      description_1 = "1",
                      description_2 = "2",
                      description_3 = "3",
                      description_4 = "4")
  )

Output:

#> # A tibble: 8 x 2
#>   Rate            WUE
#>   <fct>         <dbl>
#> 1 description_1  10.1
#> 2 description_1  10.1
#> 3 description_2  11.2
#> 4 description_2  11.2
#> 5 description_3  12.3
#> 6 description_3  12.3
#> 7 description_4  13.4
#> 8 description_4  13.4

Original table:

# A tibble: 8 x 2
   Rate   WUE
  <dbl> <dbl>
1     1  10.1
2     1  10.1
3     2  11.2
4     2  11.2
5     3  12.3
6     3  12.3
7     4  13.4
8     4  13.4

Created on 2020-11-20 by the reprex package (v0.3.0)

Eric
  • 2,699
  • 5
  • 17
2

You can use the labels argument in factor().

dataset$Rate <- factor(dataset$Rate, labels = c("A", "B", "C", "D"))

It changes label names for the levels (in the same order as levels) in the factor.
FYI, the default is factor(labels = levels). Run ?factor() for more information.

LC-datascientist
  • 1,960
  • 1
  • 18
  • 32
-1

If I understood the question right, you want to rename the levels of the Rate column. I suggest renaming like this:

data$Rate[data$Rate==1]<-"Description 1"
data$Rate[data$Rate==2]<-"Description 2"
data$Rate[data$Rate==3]<-"Description 3"
data$Rate[data$Rate==4]<-"Description 4"

And then making all factor again:

data$Rate<-as.factor(data$Rate)