如何在保持格式不变的情况下将 table/crosstab 从 r 导出到 excel
how to export table/crosstab from r to excel while keeping the format as it is
我需要在不改变 pattern/format.
crosstab() 函数是 运行 其他包的组合。从本教程中获得:http://rstudio-pubs-static.s3.amazonaws.com/6975_c4943349b6174f448104a5513fed59a9.html
注:页面获取到crosstab
函数:source("http://pcwww.liv.ac.uk/~william/R/crosstab.r")。功劳归功于 "catspec" 包中的 ctab()
函数。人们还希望获得相关联的 print.crosstab
函数,因为结果是 "crosstab" 的 class。
具体来说,我希望 excel 中的输出与使用 crosstab() 生成的高维度 table 相同,显示在 R 控制台上。
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)
我需要将交叉表格式导出为 excel,因为它是通过 print(crosstab_1) 函数在控制台中输出的。
我收到该代码的错误:
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().
我不是那个包的用户,所以决定不去找出导致错误的原因。所以我决定展示我会做些什么来实现我认为是你的目的。 base-R 中的 table
函数类似于 SPSS 中的 "crosstabs" 函数。它生成一个contingencytable,一个类似矩阵的对象:
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)
现在展开查看控制台并重复:
options( width=300)
with(Survey, table( interaction(Age, Sex), interaction(Health, Country) ) )
大部分数据在显示屏右侧不可见,但这并不重要。现在 select 用鼠标移动它,复制它,然后粘贴到电子表格的空白区域。应该出现的是一个对话框,允许指定这是固定格式的数据,并允许您轻松调整列分隔符。 Fiddle 与列并点击确定,你就完成了。
您也可以使用 capture.output
或 sink
将此输出发送到文件。
附录: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
由于是文本,您可以使用程序的固定宽度文本导入工具将其导入 excel(使用复制粘贴或 sink
或 capture.output
函数后)。它本质上与 R base ftable
函数的格式相同,可以为您提供 3 向分类。
我需要在不改变 pattern/format.
crosstab() 函数是 运行 其他包的组合。从本教程中获得:http://rstudio-pubs-static.s3.amazonaws.com/6975_c4943349b6174f448104a5513fed59a9.html
注:页面获取到crosstab
函数:source("http://pcwww.liv.ac.uk/~william/R/crosstab.r")。功劳归功于 "catspec" 包中的 ctab()
函数。人们还希望获得相关联的 print.crosstab
函数,因为结果是 "crosstab" 的 class。
具体来说,我希望 excel 中的输出与使用 crosstab() 生成的高维度 table 相同,显示在 R 控制台上。
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)
我需要将交叉表格式导出为 excel,因为它是通过 print(crosstab_1) 函数在控制台中输出的。
我收到该代码的错误:
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().
我不是那个包的用户,所以决定不去找出导致错误的原因。所以我决定展示我会做些什么来实现我认为是你的目的。 base-R 中的 table
函数类似于 SPSS 中的 "crosstabs" 函数。它生成一个contingencytable,一个类似矩阵的对象:
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)
现在展开查看控制台并重复:
options( width=300)
with(Survey, table( interaction(Age, Sex), interaction(Health, Country) ) )
大部分数据在显示屏右侧不可见,但这并不重要。现在 select 用鼠标移动它,复制它,然后粘贴到电子表格的空白区域。应该出现的是一个对话框,允许指定这是固定格式的数据,并允许您轻松调整列分隔符。 Fiddle 与列并点击确定,你就完成了。
您也可以使用 capture.output
或 sink
将此输出发送到文件。
附录: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
由于是文本,您可以使用程序的固定宽度文本导入工具将其导入 excel(使用复制粘贴或 sink
或 capture.output
函数后)。它本质上与 R base ftable
函数的格式相同,可以为您提供 3 向分类。