朱莉娅有像R的桌面功能吗?我读过xtab,但不知道如何使用它.
假设我们具有R的data.frame rdata这col6是的Factor类型.
R示例代码:
rdata <- read.csv("mycsv.csv") #1
table(rdata$col6) #2
为了在Julia中读取数据和制作因子,我这样做:
using DataFrames
jldata = readtable("mycsv.csv", makefactors=true) #1 :col6 will be now pooled.
...,但是如何在julia中构建R表(如何实现#2)?
我得出的结论是,使用by以下方法可以达到类似的效果:
让jldata由:gender列组成。
julia> by(jldata, :gender, nrow)
3x2 DataFrames.DataFrame
| Row | gender | x1 |
|-----|----------|-------|
| 1 | NA | 175 |
| 2 | "female" | 40254 |
| 3 | "male" | 58574 |
当然它不是,table但至少我得到了与数据源相同的数据类型。出人意料的by似乎比 快countmap。
您可以使用countmap函数from StatsBase.jl来计算单个变量的条目.此时缺乏列联表的一般交叉列表和统计检验.正如Ismael所指出的,这已在问题跟踪器中讨论过StatsBase.jl.
我相信,从 1.5.3 开始,“by”在 Julia 中已被贬值(它表示:错误:ArgumentError:by 函数已从 DataFrames.jl 中删除)。
\n所以这里有一些替代方案,我们也可以使用 split apply merge 来做交叉表或使用 FreqTables。
\n使用拆分组合:
\n示例 1 - SingleColumn:
\nusing RDatasets\nusing DataFrames\n\nmtcars = dataset("datasets", "mtcars")\n\n## To do a table on cyl column\n\ngdf = groupby(mtcars, :Cyl)\ncombine(gdf, nrow)\nRun Code Online (Sandbox Code Playgroud)\n输出:
\n# 3\xc3\x972 DataFrame\n# Row \xe2\x94\x82 Cyl nrow\n# \xe2\x94\x82 Int64 Int64\n# \xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n# 1 \xe2\x94\x82 6 7\n# 2 \xe2\x94\x82 4 11\n# 3 \xe2\x94\x82 8 14\nRun Code Online (Sandbox Code Playgroud)\n示例 2 - 2 列之间的交叉表:
\n## we have to just change the groupby code a little bit and rest is same\n\ngdf = groupby(mtcars, [:Cyl, :AM])\ncombine(gdf, nrow) \nRun Code Online (Sandbox Code Playgroud)\n输出:
\n#6\xc3\x973 DataFrame\n# Row \xe2\x94\x82 Cyl AM nrow\n# \xe2\x94\x82 Int64 Int64 Int64\n#\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n# 1 \xe2\x94\x82 6 1 3\n# 2 \xe2\x94\x82 4 1 8\n# 3 \xe2\x94\x82 6 0 4\n# 4 \xe2\x94\x82 8 0 12\n# 5 \xe2\x94\x82 4 0 3\n# 6 \xe2\x94\x82 8 1 2\nRun Code Online (Sandbox Code Playgroud)\n另外,如果您不喜欢顶部的 nrow 名称,您可以使用 :\n combine(gdf, nrow => :Count)\n 将名称更改为 Count
替代方法:使用 FreqTables
\n您可以使用包,FreqTables如下所示,非常轻松地进行计数和比例,添加它您可以使用Pkg.add("FreqTables"):
## Cross tab between cyl and am\nfreqtable(mtcars.Cyl, mtcars.AM)\n\n## Proportion between cyl and am\nprop(freqtable(mtcars.Cyl, mtcars.AM))\n\n## with margin like R you can use it too in this (columnwise proportion: margin=2)\n prop(freqtable(mtcars.Cyl, mtcars.AM), margins=2)\n\n## with margin for rowwise proportion: margin = 1\n prop(freqtable(mtcars.Cyl, mtcars.AM), margins=1)\nRun Code Online (Sandbox Code Playgroud)\n输出:
\n## count cross tabs\n#3\xc3\x972 Named Array{Int64,2}\n#Dim1 \xe2\x95\xb2 Dim2 \xe2\x94\x82 0 1\n#\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n#4 \xe2\x94\x82 3 8\n#6 \xe2\x94\x82 4 3\n#8 \xe2\x94\x82 12 2\n\n## proportion wise (overall)\n#3\xc3\x972 Named Array{Float64,2}\n#Dim1 \xe2\x95\xb2 Dim2 \xe2\x94\x82 0 1\n#\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n#4 \xe2\x94\x82 0.09375 0.25\n#6 \xe2\x94\x82 0.125 0.09375\n#8 \xe2\x94\x82 0.375 0.0625\n\n\n## Column wise proportion\n#3\xc3\x972 Named Array{Float64,2}\n#Dim1 \xe2\x95\xb2 Dim2 \xe2\x94\x82 0 1\n#\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n#4 \xe2\x94\x82 0.157895 0.615385\n#6 \xe2\x94\x82 0.210526 0.230769\n#8 \xe2\x94\x82 0.631579 0.153846\n\n## Row wise proportion\n#3\xc3\x972 Named Array{Float64,2}\n#Dim1 \xe2\x95\xb2 Dim2 \xe2\x94\x82 0 1\n#\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n#4 \xe2\x94\x82 0.272727 0.727273\n#6 \xe2\x94\x82 0.571429 0.428571\n#8 \xe2\x94\x82 0.857143 0.142857\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
1319 次 |
| 最近记录: |