重命名包含 R 中符号的因子水平
Rename levels of factors that contain symbols in R
我有一个名为 prueba
的数据框,其中我有一个包含以下级别的因子变量 ccaa
:
x <- c("","Andalucia","Aragon","Asturias","Balears","Canarias","Cantabria",
"Castilla Leon","Castilla Mancha","Catalu<f1>a","Ceuta","Comunitat Valenciana",
"Extremadura","Galicia","Madrid","Melilla","Murcia","Navarra","Pa<ed>s Vasco",
"Rioja")
prueba <- data.frame(cca = x)
levels(prueba$cca)
# [1] "" "Andalucia" "Aragon" "Asturias"
# [5] "Balears" "Canarias" "Cantabria" "Castilla Leon"
# [9] "Castilla Mancha" "Catalu<f1>a" "Ceuta" "Comunitat Valenciana"
# [13] "Extremadura" "Galicia" "Madrid" "Melilla"
# [17] "Murcia" "Navarra" "Pa<ed>s Vasco" "Rioja"
我正在尝试重命名那些包含 "Catalu<f1>a"
和 "Pa<ed>s Vasco"
等符号的关卡。我尝试了几个不成功的选项:
选项 1:程序包 plyr
具有函数 revalue
prueba$ccaa = revalue(prueba$ccaa,c( "Pa<ed>s Vasco" = "Basque Country", "Catalu<f1>a" = "Catalonia"))
它产生以下错误:
The following `from` values were not present in `x`: Pa<ed>s Vasco, Catalu<f1>a
选项 2:
levels(prueba$cca)[levels(prueba$cca)=="Catalu<f1>a"] <- "Catalonia"
levels(prueba$cca)[levels(prueba$cca)=="Pa<ed>s Vasco"] <- "Basque Country"
这很好用,但不会用新标签重命名关卡
levels(prueba$ccaa)
# [1] "" "Andalucia" "Aragon"
# [4] "Asturias" "Balears" "Canarias"
# [7] "Cantabria" "Castilla Leon" "Castilla Mancha"
# [10] "Catalu<f1>a" "Ceuta" "Comunitat Valenciana"
# [13] "Extremadura" "Galicia" "Madrid"
# [16] "Melilla" "Murcia" "Navarra"
# [19] "Pa<ed>s Vasco" "Rioja"
我不明白为什么关卡没有正确的标签。关于可能发生的事情有什么建议吗?
我们可以使用 recode
来自 car
library(car)
prueba$cca <- recode(prueba$cca, "'Pa<ed>s Vasco'='Basque Country';'Catalu<f1>a' = 'Catalonia'")
levels(prueba$cca)
#[1] "" "Andalucia" "Aragon" "Asturias" "Balears" "Basque Country" "Canarias"
#[8] "Cantabria" "Castilla Leon" "Castilla Mancha" "Catalonia" "Ceuta" "Comunitat Valenciana" "Extremadura"
#[15] "Galicia" "Madrid" "Melilla" "Murcia" "Navarra" "Rioja"
我有一个名为 prueba
的数据框,其中我有一个包含以下级别的因子变量 ccaa
:
x <- c("","Andalucia","Aragon","Asturias","Balears","Canarias","Cantabria",
"Castilla Leon","Castilla Mancha","Catalu<f1>a","Ceuta","Comunitat Valenciana",
"Extremadura","Galicia","Madrid","Melilla","Murcia","Navarra","Pa<ed>s Vasco",
"Rioja")
prueba <- data.frame(cca = x)
levels(prueba$cca)
# [1] "" "Andalucia" "Aragon" "Asturias"
# [5] "Balears" "Canarias" "Cantabria" "Castilla Leon"
# [9] "Castilla Mancha" "Catalu<f1>a" "Ceuta" "Comunitat Valenciana"
# [13] "Extremadura" "Galicia" "Madrid" "Melilla"
# [17] "Murcia" "Navarra" "Pa<ed>s Vasco" "Rioja"
我正在尝试重命名那些包含 "Catalu<f1>a"
和 "Pa<ed>s Vasco"
等符号的关卡。我尝试了几个不成功的选项:
选项 1:程序包 plyr
具有函数 revalue
prueba$ccaa = revalue(prueba$ccaa,c( "Pa<ed>s Vasco" = "Basque Country", "Catalu<f1>a" = "Catalonia"))
它产生以下错误:
The following `from` values were not present in `x`: Pa<ed>s Vasco, Catalu<f1>a
选项 2:
levels(prueba$cca)[levels(prueba$cca)=="Catalu<f1>a"] <- "Catalonia"
levels(prueba$cca)[levels(prueba$cca)=="Pa<ed>s Vasco"] <- "Basque Country"
这很好用,但不会用新标签重命名关卡
levels(prueba$ccaa)
# [1] "" "Andalucia" "Aragon"
# [4] "Asturias" "Balears" "Canarias"
# [7] "Cantabria" "Castilla Leon" "Castilla Mancha"
# [10] "Catalu<f1>a" "Ceuta" "Comunitat Valenciana"
# [13] "Extremadura" "Galicia" "Madrid"
# [16] "Melilla" "Murcia" "Navarra"
# [19] "Pa<ed>s Vasco" "Rioja"
我不明白为什么关卡没有正确的标签。关于可能发生的事情有什么建议吗?
我们可以使用 recode
来自 car
library(car)
prueba$cca <- recode(prueba$cca, "'Pa<ed>s Vasco'='Basque Country';'Catalu<f1>a' = 'Catalonia'")
levels(prueba$cca)
#[1] "" "Andalucia" "Aragon" "Asturias" "Balears" "Basque Country" "Canarias"
#[8] "Cantabria" "Castilla Leon" "Castilla Mancha" "Catalonia" "Ceuta" "Comunitat Valenciana" "Extremadura"
#[15] "Galicia" "Madrid" "Melilla" "Murcia" "Navarra" "Rioja"