R 如何更改现有数据集中某个因子的级别名称

R 如何更改现有数据集中某个因子的级别名称,r,R,我有以下数据集: 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

我有以下数据集:

   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

并希望将“速率”级别的名称从1-4更改为实际描述。有没有一种方法可以用代码实现这一点?我希望避免在excel中执行此操作。我在stackoverflow上使用了搜索功能,但无法找到适用于此类数据集的答案。提前感谢您的帮助,如果还有什么需要补充的,请告诉我。

您可以使用
factor()
中的labels参数


dataset$Rate这里是一个使用
dplyr
包中的
mutate
forcats
包中的
fct\u recode
的解决方案。请注意,我使用
tibble
包中的
tibble
创建了示例数据。下面是有关创建正确的应用程序的一些附加信息

mutate
允许我们修改现有的
Rate
变量

fct\u recode
允许您更改因子级别名称

as.factor
from
base r
用于将
Rate
变量强制为因子

如果您有兴趣重新排序因子级别,您应该研究使用
forcats
包中的
fct_relevel

代码:

#> # 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
# 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
库(TIBLE)
图书馆(dplyr)
图书馆(供猫用)
df%
变异(
速率=fct\U重新编码(作为系数(速率),
description_1=“1”,
description_2=“2”,
description_3=“3”,
描述_4=“4”)
)
输出:

#> # 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
# 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
#>#tible:8 x 2
#>费率WUE
#>            
#>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
原始表格:

#> # 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
# 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
#一个tible:8 x 2
费率WUE
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

由(v0.3.0)于2020年11月20日创建。

如果我正确理解了这个问题,您希望重命名Rate列的级别。 我建议改名如下:

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"
data$Rate[data$Rate==1]此函数可能会有所帮助:请参阅以下文章:和。