如何将Crosstab导出到CSV
我有一个数据集,在其中运行此代码:
source("http://pcwww.liv.ac.uk/~william/R/crosstab.r")
crosstab(nautos, row.vars = "s2_nautos_voor", col.vars = "s2_nautos_nu", type = "t")
并获得此结果:
s2_nautos_nu 0 1 2+ Sum
s2_nautos_voor
0 19.89 3.31 0.05 23.25
1 0.92 51.71 1.78 54.41
2+ 0.31 2.45 19.58 22.34
Sum 21.11 57.47 21.42 100.00
但是,当我尝试使用此代码将其保存在CSV中时:
crosstabs_nautos <- crosstab(nautos, row.vars = "s2_nautos_voor", col.vars = "s2_nautos_nu", type = "t")
write.csv(crosstabs_nautos$table,"crosstabs_nautos.csv")
它可以用type = f而不是t保存。换句话说,它不能保存该功能,我明白了:
s2_nautos_nu 0 1 2+ Sum
s2_nautos_voor
0 390 65 1 456
1 18 1014 35 1067
2+ 6 48 384 438
Sum 414 1127 420 1961
...无论我应用哪个crosstab函数。
我该如何解决?
I have a dataset, where I run this code:
source("http://pcwww.liv.ac.uk/~william/R/crosstab.r")
crosstab(nautos, row.vars = "s2_nautos_voor", col.vars = "s2_nautos_nu", type = "t")
And get this result:
s2_nautos_nu 0 1 2+ Sum
s2_nautos_voor
0 19.89 3.31 0.05 23.25
1 0.92 51.71 1.78 54.41
2+ 0.31 2.45 19.58 22.34
Sum 21.11 57.47 21.42 100.00
However, when I try to save it in a csv using this code:
crosstabs_nautos <- crosstab(nautos, row.vars = "s2_nautos_voor", col.vars = "s2_nautos_nu", type = "t")
write.csv(crosstabs_nautos$table,"crosstabs_nautos.csv")
It gets saved with the type = f instead of t. In other words, it doesn't save the function, and I get this:
s2_nautos_nu 0 1 2+ Sum
s2_nautos_voor
0 390 65 1 456
1 18 1014 35 1067
2+ 6 48 384 438
Sum 414 1127 420 1961
...no matter which crosstab function I apply.
How can I get around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜想
crosstabs
函数仅将表作为频率存储在类crosstabs的对象中,然后用相关的S3print.crosstabs 函数在“决定”是打印原始频率还是百分比之前检查类型的功能。如果您仅将列表结构的“表”叶子发送到write.csv,则没有机会检查该类型。也许您应该通过实验或阅读代码来弄清楚这一点。我在该学术网站上找不到有关这组功能的任何文档。如果您有与文档的任何链接,则应将它们包括在问题中。
同样,代码中的大多数检查都是theSort的:
因此,它们是确切的检查,并且拼写需要是确切的,而不是缩写。您可以使用
capture.output
将print.crosstabs
的结果发送到文件。它不会被划界。但是,这可能不是问题,因为您关注的输出似乎没有逗号。尝试或您可以写入。CSV使用对象
crosstab_nautos $ table/1961
编辑:在crosstabs函数末尾附近有一些代码,使我认为我的猜测并不完全正确:
I'm guessing that the
crosstabs
function only stores the table as frequencies in an object of class crosstabs and then "prints" with desired type to the console with an associated S3print.crosstabs
function that checks for the type before "deciding" whether to print raw frequencies or percentages. If you only send the "table" leaf of the list structure to write.csv, then there is no opportunity to check the type. Perhaps you are expected to figure this out by experimentation or by reading the code. I cannot find any documentation of that set of functions at that academic website. If you have any links to documentation, then you should include them in your question.Also, most of the checks in the code are of thesort:
So they are written to be exact checks and the spelling would need to be exact rather than abbreviated. You could use
capture.output
to send the results ofprint.crosstabs
to a file. It won't be csv delimited. However that might not be a problem since there don't appear to be commas in the output you are concerned about. TryOr you could perhaps write.csv using the object
crosstab_nautos$table/1961
Edit: There's some code near the end of the crosstabs function that's making me think my guesses were not completely correct: