例如,我有一个名为"动物";。animals=read_csv("animals.csv"(
它包含一个名为";物种";,其中Species的值可以是";a"b"c";。请参见下文。
Weight Height Species
<dbl> <dbl> <chr>
1 12.3 15.7 a
2 8.1 17.9 a
3 11.3 14.4 b
4 12.1 18 b
5 6.8 18.1 a
6 16.3 18.3 a
7 19.6 19.2 c
8 22.2 19 c
现在我想重命名a,b,c的值。a是";老虎";,b是";狮子";,c是";大象;所以它变成了下面。我需要使用因子函数来重命名它们,请问我该怎么做?
Weight Height Species
<dbl> <dbl> <chr>
1 12.3 15.7 tiger
2 8.1 17.9 tiger
3 11.3 14.4 lion
4 12.1 18 lion
5 6.8 18.1 tiger
6 16.3 18.3 tiger
7 19.6 19.2 elephant
8 22.2 19 elephant
以下是使用factor
的基本R选项
transform(
df,
Species = c('tiger', 'lion', 'elephant')[as.integer(factor(Species,levels = c('a', 'b', 'c')))]
)
它给出
Weight Height Species
1 12.3 15.7 tiger
2 8.1 17.9 tiger
3 11.3 14.4 lion
4 12.1 18.0 lion
5 6.8 18.1 tiger
6 16.3 18.3 tiger
7 19.6 19.2 elephant
8 22.2 19.0 elephant
这里有一个left_join
选项
keydat <- data.frame(Species = c('a', 'b', 'c'),
value = c('tiger', 'lion', 'elephant'))
library(dplyr)
df1 <- df1 %>%
left_join(keydat) %>%
mutate(Species = value, value = NULL)
-输出
df1
# Weight Height Species
#1 12.3 15.7 tiger
#2 8.1 17.9 tiger
#3 11.3 14.4 lion
#4 12.1 18.0 lion
#5 6.8 18.1 tiger
#6 16.3 18.3 tiger
#7 19.6 19.2 elephant
#8 22.2 19.0 elephant
或者使用命名向量来匹配和替换
nm1 <- setNames(c('tiger', 'lion', 'elephant'),
c('a', 'b', 'c'))
df1$Species <- nm1[df1$Species]
如果列为factor
(也应与character
类一起使用(,则使用fct_recode
选项
library(forcats)
df1$Species <- fct_recode(factor(df1$Species), !!!setNames(names(nm1), nm1) )
数据
df1 <- structure(list(Weight = c(12.3, 8.1, 11.3, 12.1, 6.8, 16.3, 19.6,
22.2), Height = c(15.7, 17.9, 14.4, 18, 18.1, 18.3, 19.2, 19),
Species = c("a", "a", "b", "b", "a", "a", "c", "c")), class = "data.frame",
row.names = c("1",
"2", "3", "4", "5", "6", "7", "8"))
如果Species
还不是一个因子,可以通过animals$Species = as.factor(animals$Species)
将其转化为一个因子
然后,您可以通过以下操作更改级别:
levels(animals$Species) = c("tiger","lion","elephant")