在R中使用XLSX软件包在Excel中打印data.frame时出错 [英] Error in printing data.frame in excel using XLSX package in R

查看:410
本文介绍了在R中使用XLSX软件包在Excel中打印data.frame时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据框可见,没有任何错误.但是,当使用软件包XLSX的write.xlsx功能打印相同内容时,则会出现错误.

The dataframe is visible with out any error. But when the same is printed using write.xlsx fucnction of the package XLSX, it gives the error.

Error in .jcall(cell, "V", "setCellValue", value) : 
  method setCellValue with signature ([D)V not found.

data.frame的输出看起来像:

The dput of the data.frame looks like:

Timestamp         qs          pqs        logqs         es         p_imp      dep    r_dep       agg_rtn
               (time)      (dbl)        (dbl)        (dbl)      (dbl)         (dbl)    (dbl)    (dbl)         (dbl)
1 2015-05-04 09:29:59 0.05788732 0.0007478696 0.0007478545 0.09633803 -0.0446830986 3533.518 274079.9 -0.0006432937
2 2015-05-04 10:00:00 0.04948394 0.0006362707 0.0006362707 0.07586009  0.0088016055 2416.431 187953.1  0.0000000000
3 2015-05-04 10:30:00 0.05554795 0.0007142532 0.0007142532 0.06417808 -0.0002739726 3245.574 252422.0  0.0000000000
4 2015-05-04 10:59:59 0.04863014 0.0006194244 0.0006194244 0.08434442  0.0024951076 3563.401 279503.9  0.0000000000
5 2015-05-04 11:30:00 0.05761986 0.0007319037 0.0007319037 0.07851027  0.0154965753 2010.943 158429.1 -0.0006339144
6 2015-05-04 12:00:00 0.04957627 0.0006285051 0.0006285051 0.07025424  0.0070762712 1819.908 143546.0  0.0000000000
Variables not shown: vol_30_sum (dbl), vol_30_mean (dbl), p_return_sqr (dbl), p_return_mean (dbl), Lim_or_out (dbl),
  closing_price (dbl), closing_vol (dbl)

请帮助解决此错误.

推荐答案

仍然没有可复制的示例,但是从您的class(q1)看来,q1tbl_df(是dplyr包的一种数据框.产生),而write.xlsx期望为data.frame.

Still no reproducible example, but from your class(q1) it appears that q1 is a tbl_df (the sort of dataframe that the dplyr package produces) whereas write.xlsx expects a data.frame.

尝试按预期给write.xlsx一个普通的data.frame.例如

Try giving write.xlsx a plain data.frame as it expects. e.g.

write.xlsx(as.data.frame(q1), ...)

这是一个 可复制的示例 (即,您可以将其复制粘贴到R会话中以重现该错误+修复).

Here's a reproducible example (i.e. you could copy-paste it into your R session to reproduce the bug + fix).

library(dplyr)
iris2 <- tbl_df(iris)
class(iris2) # like yours
# [1] "tbl_df"     "tbl"        "data.frame" 

# Now let's try to write to XLSX using command as mentioned in your comments
library(xlsx)
write.xlsx(iris2, file='test.xlsx', sheetName="Sheet1", col.names=TRUE, row.names=FALSE, append=TRUE)
# Error in .jcall(cell, "V", "setCellValue", value) : 
#   method setCellValue with signature ([D)V not found
# In addition: Warning message:
# In if (is.na(value)) { :
#  the condition has length > 1 and only the first element will be used
# ^--- we can reproduce your error. This is the point of a reproducible example, so we can see if our fixes work for you.

现在让我们尝试通过确保write.xlsx获得一个data.frame而不是tbl_df来解决它!

Now let's try fix it by making sure that write.xlsx gets a data.frame, not a tbl_df!

write.xlsx(as.data.frame(iris2), file='test.xlsx', sheetName="Sheet1", col.names=TRUE, row.names=FALSE, append=TRUE)
# huzzah!

这篇关于在R中使用XLSX软件包在Excel中打印data.frame时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆