gpt4 book ai didi

将 write.xlsx 与使用 'dplyr' 函数创建的对象一起使用时出现 R 错误

转载 作者:行者123 更新时间:2023-12-04 18:02:15 25 4
gpt4 key购买 nike

我在尝试使用 xlsx 包中的函数 write.xlsx 将数据框写入 excel 文件时遇到问题。尽管问题仅在使用 dplyr 包中的函数创建数据框时出现。当我使用基函数时,没有问题。下面是一个最小的例子。

首先是示例数据:

library(dplyr)
library(xlsx)
month <- c('Julio','Diciembre','Diciembre','Agosto','Noviembre',
'Diciembre', 'Junio','Septiembre','Agosto','Julio')

irrelevant_column <- rep(1,10)

df <- as.data.frame(cbind(irrelevant_column, month))

正如我所说,当我使用基本函数时没有问题:

month1  <- table(df$month, df$irrelevant_column)
month1 <- prop.table(month1 , 2)
month1 <- as.data.frame.matrix(month1 )
write.xlsx(month1 , file="month1.xlsx")

没有出现错误,但是当我使用“dplyr”创建类似的数据框时:

month2<- count(df, month)
month2<- mutate(month2, porc = n / sum(month2[, 2]))
month2<- as.data.frame.matrix(month2)
write.xlsx(month2, file="month2.xlsx")

出现以下错误信息:

Error in .jcall(cell, "V", "setCellValue", value) : 
method setCellValue with signature ([Ljava/lang/String;)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

有解决办法吗,还是xlsxdplyr不兼容?

最佳答案

遇到这个问题并使用 as.data.frame() 将 group_by() %>% summarize() 的 tibble 输出指定为 df 来解决它。

关于将 write.xlsx 与使用 'dplyr' 函数创建的对象一起使用时出现 R 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33139087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com