使用数据框后保持变量类型
Keep variables type after using data frame
我正在尝试使用 R 包 clustMixType
中的 kproto()
函数在 Julia 中对混合类型数据进行聚类,但出现错误 No numeric variables in x! Try using kmodes() from package...
。我的数据应该有 3 个变量:2 个连续变量和 1 个分类变量。似乎在我使用 DataFrame()
之后,所有变量都变得明确了。有没有办法避免在使用 DataFrame()
后更改变量类型,以便我有混合类型的数据(连续和分类)来使用 kproto()
?
using RCall
@rlibrary clustMixType
# group 1 variables
x1=rand(Normal(0,3),10)
x2=rand(Normal(1,2),10)
x3=["1","1","2","2","0","1","1","2","2","0"]
g1=hcat(x1,x2,x3)
# group 2 variables
y1=rand(Normal(0,4),10)
y2=rand(Normal(-1,6),10)
y3=["1","1","2","1","1","2","2","0","2","0"]
g2=hcat(y1,y2,y3)
#create the data
df0=vcat(g1,g2)
df1 = DataFrame(df0)
#use R function
R"kproto($df1, 2)"
我对 R 包及其期望的输入类型一无所知,但问题可能在于如何构建数据矩阵,而不是 DataFrame 构造函数本身。
当您连接一个数字列和一个字符串列时,Julia 会返回结果矩阵的元素类型 Any
:
julia> g1=hcat(x1,x2,x3)
10×3 Matrix{Any}:
0.708309 -4.84767 "1"
0.566883 -0.214217 "1"
...
这意味着您的 df0
矩阵是:
julia> #create the data
df0=vcat(g1,g2)
20×3 Matrix{Any}:
0.708309 -4.84767 "1"
0.566883 -0.214217 "1"
...
和 DataFrame
构造函数将只携带这种类型信息的缺失,而不是试图推断列类型。
julia> DataFrame(df0)
20×3 DataFrame
Row │ x1 x2 x3
│ Any Any Any
─────┼───────────────────────────
1 │ 0.708309 -4.84767 1
2 │ 0.566883 -0.214217 1
...
解决这个问题的一个简单方法是不将您的列连接成一个矩阵,而是从列构建 DataFrame:
julia> DataFrame([vcat(x1, y1), vcat(x2, y2), vcat(x3, y3)])
20×3 DataFrame
Row │ x1 x2 x3
│ Float64 Float64 String
─────┼───────────────────────────────
1 │ 0.708309 -4.84767 1
2 │ 0.566883 -0.214217 1
...
如您所见,我们现在在生成的 DataFrame 中有两个 Float64
数值列 x1
和 x2
。
作为对 Nils 的好回答的补充(因为问题确实是在构造矩阵时而不是在创建 DataFrame
时)有这个小技巧:
julia> df = DataFrame([1 1.0 "1"; 2 2.0 "2"], [:int, :float, :string])
2×3 DataFrame
Row │ int float string
│ Any Any Any
─────┼────────────────────
1 │ 1 1.0 1
2 │ 2 2.0 2
julia> identity.(df)
2×3 DataFrame
Row │ int float string
│ Int64 Float64 String
─────┼────────────────────────
1 │ 1 1.0 1
2 │ 2 2.0 2
我正在尝试使用 R 包 clustMixType
中的 kproto()
函数在 Julia 中对混合类型数据进行聚类,但出现错误 No numeric variables in x! Try using kmodes() from package...
。我的数据应该有 3 个变量:2 个连续变量和 1 个分类变量。似乎在我使用 DataFrame()
之后,所有变量都变得明确了。有没有办法避免在使用 DataFrame()
后更改变量类型,以便我有混合类型的数据(连续和分类)来使用 kproto()
?
using RCall
@rlibrary clustMixType
# group 1 variables
x1=rand(Normal(0,3),10)
x2=rand(Normal(1,2),10)
x3=["1","1","2","2","0","1","1","2","2","0"]
g1=hcat(x1,x2,x3)
# group 2 variables
y1=rand(Normal(0,4),10)
y2=rand(Normal(-1,6),10)
y3=["1","1","2","1","1","2","2","0","2","0"]
g2=hcat(y1,y2,y3)
#create the data
df0=vcat(g1,g2)
df1 = DataFrame(df0)
#use R function
R"kproto($df1, 2)"
我对 R 包及其期望的输入类型一无所知,但问题可能在于如何构建数据矩阵,而不是 DataFrame 构造函数本身。
当您连接一个数字列和一个字符串列时,Julia 会返回结果矩阵的元素类型 Any
:
julia> g1=hcat(x1,x2,x3)
10×3 Matrix{Any}:
0.708309 -4.84767 "1"
0.566883 -0.214217 "1"
...
这意味着您的 df0
矩阵是:
julia> #create the data
df0=vcat(g1,g2)
20×3 Matrix{Any}:
0.708309 -4.84767 "1"
0.566883 -0.214217 "1"
...
和 DataFrame
构造函数将只携带这种类型信息的缺失,而不是试图推断列类型。
julia> DataFrame(df0)
20×3 DataFrame
Row │ x1 x2 x3
│ Any Any Any
─────┼───────────────────────────
1 │ 0.708309 -4.84767 1
2 │ 0.566883 -0.214217 1
...
解决这个问题的一个简单方法是不将您的列连接成一个矩阵,而是从列构建 DataFrame:
julia> DataFrame([vcat(x1, y1), vcat(x2, y2), vcat(x3, y3)])
20×3 DataFrame
Row │ x1 x2 x3
│ Float64 Float64 String
─────┼───────────────────────────────
1 │ 0.708309 -4.84767 1
2 │ 0.566883 -0.214217 1
...
如您所见,我们现在在生成的 DataFrame 中有两个 Float64
数值列 x1
和 x2
。
作为对 Nils 的好回答的补充(因为问题确实是在构造矩阵时而不是在创建 DataFrame
时)有这个小技巧:
julia> df = DataFrame([1 1.0 "1"; 2 2.0 "2"], [:int, :float, :string])
2×3 DataFrame
Row │ int float string
│ Any Any Any
─────┼────────────────────
1 │ 1 1.0 1
2 │ 2 2.0 2
julia> identity.(df)
2×3 DataFrame
Row │ int float string
│ Int64 Float64 String
─────┼────────────────────────
1 │ 1 1.0 1
2 │ 2 2.0 2