Julia 如何改变向量的levels

大家好,我想改变一个向量的levels,在R语言中:

> Sire = c("ZA","AD","BB","AD","AD","CC","CC","AD","AD")
> Sire = as.factor(Sire)
> levels(Sire)
[1] "AD" "BB" "CC" "ZA"
> levels(Sire) = 1:4
> Sire
[1] 4 1 2 1 1 3 3 1 1
Levels: 1 2 3 4

在Julia中,我找到了levels的函数,但是确无法修改,

a1= ["ZA","AD","BB","AD","AD","CC","CC","AD","AD"]

报错如下:

julia> Sire = ["ZA","AD","BB","AD","AD","CC","CC","AD","AD"];

julia> levels(Sire)
4-element Array{String,1}:
 "AD"
 "BB"
 "CC"
 "ZA"

julia> levels(Sire) = [1,2,3,4]
ERROR: error in method definition: function Missings.levels must be explicitly imported to be extended
Stacktrace:
 [1] top-level scope at none:0

我的问题,如何改变一个向量的levels,十分感谢。

你可能需要

http://juliadata.github.io/CategoricalArrays.jl/stable/using.html

julia 这边标准库只有一些简单的数据结构,复杂的要用其他的包

julia> # ]add CategoricalArrays

julia> using CategoricalArrays

julia> Sire = ["ZA","AD","BB","AD","AD","CC","CC","AD","AD"];

julia> levels(Sire)
4-element Array{String,1}:
 "AD"
 "BB"
 "CC"
 "ZA"

julia> d = Dict(zip(levels(Sire), 1:4))
Dict{String,Int64} with 4 entries:
  "BB" => 2
  "AD" => 1
  "CC" => 3
  "ZA" => 4

julia> s2 = recode(Sire, d...)
9-element Array{Int64,1}:
 4
 1
 2
 1
 1
 3
 3
 1
 1

julia> levels(s2)
4-element Array{Int64,1}:
 1
 2
 3
 4

ref:

1 个赞

我进行了一个汇总:

Sire = ["ZA","AD","BB","AD","AD","CC","CC","AD","AD"]

# methods 1
a1 = deepcopy(Sire)
new = collect(1:length(levels(Sire)))
d = Dict(zip(levels(a1),new))
using CategoricalArrays
re1 = recode(a1,d...)

# methods 2
a2 = deepcopy(Sire)
new = collect(1:length(levels(Sire)))
un = unique(a2)
replace(a2, Pair.(un, axes(un, 1))...)


# methods 3
a3 = deepcopy(Sire)
new = collect(1:length(levels(a3)))
d = Dict(zip(levels(a3),new))
re3 = replace(a3,d...)

参考:

1 个赞