【问题标题】:Exporting a tabular type object into a spreadsheet将表格类型对象导出到电子表格
【发布时间】:2020-06-23 15:49:57
【问题描述】:

我有这样的数据:

require(tables)
varList <- 2:4
lapply(varList,function(x,df,byVar){ 
         tabular((Factor(df[[x]],paste(colnames(df)[x])) + 1) ~ ((Factor(df[[byVar]],paste(byVar)))*((n=1) + Percent("col"))),
             data= df) 
  },mtcars,"cyl")

我想将此数据导出到电子表格中(现在,我正在将其复制粘贴到 Excel 中)。

当我尝试将其导出到 excel 时出现此错误:

Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : 
cannot coerce class ‘"tabular"’ to a data.frame

感谢任何帮助!

【问题讨论】:

  • 如果您包含一个简单的reproducible example,其中包含可用于测试和验证可能解决方案的示例输入和所需输出,则会更容易为您提供帮助。

标签: r tabular


【解决方案1】:

tables 包中有一个名为write.csv.tabular 的函数,但是在您的示例中,lapply 语句中的表格数据被转换为类list,因此您实际上是在使用列表of 个表格对象,这与直接处理表格数据有很大不同。

class(data)
[1] "list"

list 包含 3 个表格对象,但它仍然是 list 类,这会混淆导出功能。

我将展示如何直接写出tabular 数据,然后继续使用tabular 对象中的list

以下是从iris 数据集创建的表格数据示例:

require(tables)

tabular_data <- tabular( (Species + 1) ~ (n=1) + Format(digits=2)*
                                       (Sepal.Length + Sepal.Width)*(mean   + sd), data=iris )

write.csv.tabular(tabular_data, "tabular_data.csv")

回到最初的示例,我们需要在 R 中使用 Excel 包,例如 xlsx,并将列表项类从 tabular 转换为它可以处理的内容。由于tabular 不会直接转换为data.frame,我们可以使用data.frame.matrix,但需要做更多的工作来恢复您的列标题。

首先不用担心列标题:

library(tables)
library(xlsx)

varList <- 2:4
data    <- lapply(varList,function(x,df,byVar){ 
  tabular((Factor(df[[x]],paste(colnames(df)[x])) + 1) ~ 
            ((Factor(df[[byVar]],paste(byVar)))*((n=1) + Percent("col"))),
          data= df) 
},mtcars,"cyl")

data <- lapply(data, as.data.frame.matrix) 

file <- paste("your_workbook.xlsx", sep = "")
write.xlsx(data[[1]], file, sheetName = "Sheet1") 
write.xlsx(data[[2]], file, sheetName = "Sheet2", append = TRUE)
write.xlsx(data[[3]], file, sheetName = "Sheet3", append = TRUE)

现在,就列标题而言,让我们看一下您的一个表格列表项,我们会发现一开始确实没有创建正确的列名:

varList <- 2:4
data    <- lapply(varList,function(x,df,byVar){ 
  tabular((Factor(df[[x]],paste(colnames(df)[x])) + 1) ~ 
            ((Factor(df[[byVar]],paste(byVar)))*((n=1) + Percent("col"))),
          data= df) 
},mtcars,"cyl")

     cyl                             
     4           6         8         
 cyl n   Percent n Percent n  Percent
 4   11  100     0   0      0   0    
 6    0    0     7 100      0   0    
 8    0    0     0   0     14 100    
 All 11  100     7 100     14 100   

colLabels 属性是一个 3 行矩阵,而 colnames 只能是每列 1 个元素。所以,我们必须解决它。

?colLabels 中的示例只是将其子集为这 3 行中的 1 行的值,这使得某些列没有标题,即

colLabels(data[[1]]) <- colLabels(data)[1,]

colLabels(data[[1]]) <- colLabels(data)[3,]

等等

pastegsub 可用于创建更完整的列名集,但没有将它们转换为特殊的 colLabels 类的功能,因此您希望将它们保留为字符串对象然后在将您的数据转换为data.frame.matrixdata.frame 或导出到 Excel 后,将相应工作表的字符串设置为 colnames

labels <- paste(colLabels(data[[1]])[1,],
                colLabels(data[[1]])[2,],
                colLabels(data[[1]])[3,])
labels <- gsub("NA ","",labels)

【讨论】:

  • 导出的数据格式不合理。我想查看列名等,
  • 对不起,我的数据与我在问题中发布的数据非常相似,我猜它是一个多列表对象,我认为它也被认为是表格的。
  • 谢谢!运行时出现错误:write.xlsx(data[[1]], file, sheetName = "Sheet1") .... is.nan(tmp) 中的错误:未为类型“list”实现默认方法。
  • @NewBee 当你使用write.xlsx 它应该已经被data &lt;- lapply(data, as.data.frame.matrix)从列表转换为data.frame.matrix
【解决方案2】:

您可能希望将每个表写入其自己的 csv 以保持表格对象的结构:

lapply(1:length(a), function(x) write.csv.tabular(a[[x]], file = paste0("table_", x, ".csv")))

【讨论】:

    猜你喜欢
    • 2012-09-16
    • 1970-01-01
    • 2018-10-26
    • 2013-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    相关资源
    最近更新 更多