如何将Crosstab导出到CSV

发布于 2025-01-17 14:01:28 字数 1191 浏览 1 评论 0原文

我有一个数据集,在其中运行此代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缱倦旧时光 2025-01-24 14:01:28

我猜想crosstabs函数仅将表作为频率存储在类crosstabs的对象中,然后用相关的S3 print.crosstabs 函数在“决定”是打印原始频率还是百分比之前检查类型的功能。如果您仅将列表结构的“表”叶子发送到write.csv,则没有机会检查该类型。也许您应该通过实验或阅读代码来弄清楚这一点。我在该学术网站上找不到有关这组功能的任何文档。如果您有与文档的任何链接,则应将它们包括在问题中。

同样,代码中的大多数检查都是theSort的:

if ( type="frequency") {   do something

因此,它们是确切的检查,并且拼写需要是确切的,而不是缩写。您可以使用capture.outputprint.crosstabs的结果发送到文件。它不会被划界。但是,这可能不是问题,因为您关注的输出似乎没有逗号。尝试

 capture.output( print(crosstabs_nautos, type = "frequencies") , 
               file="crosstabs_nautos.txt")

或您可以写入。CSV使用对象crosstab_nautos $ table/1961

编辑:在crosstabs函数末尾附近有一些代码,使我认为我的猜测并不完全正确:

 #(b) tabular output [3 variants]
    result$table <- tbl  #Stores original cross-tab frequency counts without margins [class: table]
    result$crosstab <- crosstab #Stores cross-tab in table format using requested style(frequency/pct) and table margins (on/off)
                                #[class: table]  
    result$crosstab.nosub <- t1  #crosstab with subtotals suppressed [class: dataframe; or NULL if no subtotals suppressed]  
    class(result) <- "crosstab"    

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 S3 print.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:

if ( type="frequency") {   do something

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 of print.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. Try

 capture.output( print(crosstabs_nautos, type = "frequencies") , 
               file="crosstabs_nautos.txt")

Or 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:

 #(b) tabular output [3 variants]
    result$table <- tbl  #Stores original cross-tab frequency counts without margins [class: table]
    result$crosstab <- crosstab #Stores cross-tab in table format using requested style(frequency/pct) and table margins (on/off)
                                #[class: table]  
    result$crosstab.nosub <- t1  #crosstab with subtotals suppressed [class: dataframe; or NULL if no subtotals suppressed]  
    class(result) <- "crosstab"    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文