简体   繁体   English

如何将表格/交叉表从r导出到excel,同时保持格式不变

[英]how to export table/crosstab from r to excel while keeping the format as it is

I need to export a high dimension frequency table generated by crosstab() (Package: descr) on 2 rows and 2 column variables from R to excel without changing the pattern/format. 我需要在不更改模式/格式的情况下,将R上2行2列变量的crosstab()(包:descr)生成的高维频率表导出到excel。

The crosstab() function is running with combination of other packages. crosstab()函数与其他软件包的组合一起运行。 Got from this tutorial: http://rstudio-pubs-static.s3.amazonaws.com/6975_c4943349b6174f448104a5513fed59a9.html 从本教程获得: http : //rstudio-pubs-static.s3.amazonaws.com/6975_c4943349b6174f448104a5513fed59a9.html

Note: The page obtained the crosstab function with: source(" http://pcwww.liv.ac.uk/~william/R/crosstab.r "). 注意:该页面通过以下方式获得了crosstab功能:source(“ http://pcwww.liv.ac.uk/~william/R/crosstab.r ”)。 Credit was given to the ctab() function in the "catspec" package. 感谢“ catspec”包中的ctab ctab()函数。 One would be expected to also obtain the associated print.crosstab function, because the result is given a class of "crosstab". 因为结果被赋予一类“ crosstab”,所以人们也有望获得相关的print.crosstab函数。

Specifically, I want the same out-put in excel as the high dimension table generated using crosstab() is displayed on R console. 具体来说,我希望Excel中的输出与在R控制台上显示使用crosstab()生成的高维表相同。

install.packages("descr")
library(descr)
ID <- seq(1:177)
Age <- sample(c("0-15", "16-29", "30-44", "45-64", "65+"), 177, replace = TRUE)
Sex <- sample(c("Male", "Female"), 177, replace = TRUE)
Country <- sample(c("England", "Wales", "Scotland", "N. Ireland"), 177, replace = TRUE)
Health <- sample(c("Poor", "Average", "Good"), 177, replace = TRUE)
Survey <- data.frame(Age, Sex, Country, Health)
head(Survey)
crosstab_1 <- crosstab(Survey, row.vars = c("Age", "Sex"), col.vars = c("Health", "Country"), 
     type = "f", addmargins = FALSE)
print(crosstab_1)

I need to export to excel the crosstab format as it is comes out in console through print(crosstab_1) function. 我需要将交叉表格式导出为ex​​cel,因为它是通过print(crosstab_1)函数在控制台中显示的。

I get an error with that code: 我收到该代码错误:

Error in crosstab(Survey, row.vars = c("Age", "Sex"), col.vars = c("Health",  : 
  The 'indep' (independent variable) is missing. Please, consider using either CrossTable() or freq().

I'm not a user of that package so decided against figuring out what caused the error. 我不是该程序包的用户,所以决定不要弄清楚是什么原因导致了错误。 So I decided to show what I would have done to achieve what I think is your purpose. 因此,我决定向我展示要实现我认为是您的目标的目标。 The table function in base-R is like the "crosstabs" function in SPSS. base-R中的table功能类似于SPSS中的“交叉表”功能。 It generates a contingency table, a matrix-like object: 它生成一个列联表,一个类似矩阵的对象:

with(Survey, table( interaction(Age, Sex), interaction(Health, Country) ) )
#-----

               Average.England Good.England Poor.England
  0-15.Female                0            0            2
  16-29.Female               1            3            4
  30-44.Female               2            3            0
  45-64.Female               1            1            0
  65+.Female                 2            0            4
  0-15.Male                  1            3            1
  16-29.Male                 0            2            1
  30-44.Male                 2            3            1
  45-64.Male                 2            2            0
  65+.Male                   3            3            1

               Average.N. Ireland Good.N. Ireland Poor.N. Ireland
  0-15.Female                   1               2               0
  16-29.Female                  1               0               1
  30-44.Female                  2               0               1
  45-64.Female                  1               1               2

snipped the rest of the output since it will clearly not be amenable to easy processing with Excel (or in my case OpenOffice) 删除了其余的输出,因为显然不适合使用Excel(或者我的情况是OpenOffice)轻松处理

Now expand the viewing console and repeat: 现在,展开查看控制台并重复:

options( width=300)
with(Survey, table( interaction(Age, Sex), interaction(Health, Country) ) )

Most of the data is not visible on hte right side of the display but that doesn't matter. 大多数数据在显示屏的右侧不可见,但这无关紧要。 Now select it with your cursor-mousing, copy-, then paste into a blank region of your spreadsheet. 现在,使用鼠标光标进行选择,复制,然后将其粘贴到电子表格的空白区域。 What should appear is a dialog that allows specifying that this is fixed format data and allows you to adjust the column separators easily. 应该出现的对话框允许您指定这是固定格式的数据,并允许您轻松调整列分隔符。 Fiddle with the columns and hit OK and you're done. 轻敲列,然后单击“确定”,就完成了。

You could also have used capture.output or sink to send this output to a file. 您还可能使用capture.output或接收sink将此输出发送到文件。

Addendum: The output of the print.crosstabs function from the site listed below in the comments is: 附录:下面注释中列出的站点的print.crosstabs函数的输出为:

print(crosstab_1)
#----------------------------
             Health  Average                              Good                              Poor                          
             Country England N. Ireland Scotland Wales England N. Ireland Scotland Wales England N. Ireland Scotland Wales
Age   Sex                                                                                                                 
0-15  Female               2          1        4     1       3          2        1     3       0          3        1     1
      Male                 1          0        1     0       0          2        1     1       1          1        1     3
16-29 Female               1          1        1     3       1          1        2     2       0          2        0     1
      Male                 4          0        2     1       1          5        0     1       2          2        2     0
30-44 Female               2          1        2     3       2          2        1     2       1          2        1     3
      Male                 1          2        3     1       1          3        3     3       2          0        3     1
45-64 Female               1          0        1     3       3          0        4     1       0          0        1     0
      Male                 2          0        1     3       1          2        2     0       3          2        0     0
65+   Female               1          1        3     0       1          1        1     3       3          6        1     0
      Male                 0          1        1     0       3          0        0     1       1          2        1     2

Since is is text, you could import it into excel (after using copy-paste or the sink or capture.output functions) using the program's fixed width text import facilities. 由于是文本,因此您可以使用程序的固定宽度文本导入工具将其导入excel(使用复制粘贴或接收sinkcapture.output函数后)。 It's essentially the same format as R base ftable function would give you for a 3-way classification. 它与R base ftable函数为您提供3向分类的格式基本相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM