2016-08-31 42 views
2

我正在使用dplyr创建一个对象,然后我使用xlsx写出一个扩展名。无法在R中使用xlsx包编写excel文件

我运行下面的代码:

provFundedProp <- compensationBase2014 %>% 
group_by(provinciallyFunded) %>% 
summarise(total=sum(fundingRaw)) %>% 
mutate(percent = paste0(round(100 * total/sum(total),1), "%")) 

然后我写第一张:

write.xlsx(provFundedProp, file="output/provFundedProp.xlsx",  
sheetName="provFundingSector") 

这工作得很好,给我我需要的文件。

我然后运行下面的代码会下降一个等级:

provFundedServiceDivision <- compensationBase2014 %>% 
group_by(serviceDivision,provinciallyFunded) %>% 
summarise(total=sum(fundingRaw)) %>% 
mutate(percent = paste0(round(100 * total/sum(total),1), "%")) 

#write to second sheet 
write.xlsx(provFundedServiceDivision, file="output/provFundedSD.xlsx", 
sheetName="provFundingSD") 

使我有以下错误:

Error: cannot convert object to a data frame 

我快要疯了这里。有没有人知道到底发生了什么? 我已经尝试过这与多个wueries,我不知道什么是。

class(provFundedServiceDivision) [1] "grouped_df" "tbl_df" "tbl" 
"data.frame" 

Classes ‘grouped_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 6 obs. of 4 
variables: 
$ serviceDivision : chr "AS" "AS" "CLS" "CLS" ... 
$ provinciallyFunded: chr "NPF" "PF" "NPF" "PF" ... 
$ total    : num 1.90e+06 3.97e+07 2.93e+07 5.70e+08 9.55e+07 ... 
$ percent   : chr "4.6%" "95.4%" "4.9%" "95.1%" ... 
- attr(*, "vars")=List of 1 
..$ : symbol serviceDivision 
- attr(*, "labels")='data.frame': 3 obs. of 1 variable: 
..$ serviceDivision: chr "AS" "CLS" "GS" 
..- attr(*, "vars")=List of 1 
.. ..$ : symbol serviceDivision 
..- attr(*, "drop")= logi TRUE 
- attr(*, "indices")=List of 3 
..$ : int 0 1 
..$ : int 2 3 
..$ : int 4 5 
- attr(*, "drop")= logi TRUE 
- attr(*, "group_sizes")= int 2 2 2 
- attr(*, "biggest_group_size")= int 2 

> traceback() 
7: stop(list(message = "cannot convert object to a data frame", 
call = NULL, cppstack = NULL)) 
6: .Call("dplyr_cbind_all", PACKAGE = "dplyr", dots) 
5: cbind_all(x) 
4: bind_cols(...) 
3: cbind(deparse.level, ...) 
2: cbind(rownames = rownames(x), x) 
1: write.xlsx(provFundedServiceDivision, file = "output/provFundedSD.xlsx", 
sheetName = "provFundingSD") 
+0

这是什么回报.. 。class(provFundedServiceDivision)' –

+0

至少,你可以提供'str(provFundedSer viceDivision)'。在得到错误之后,甚至可能是“traceback()”的结果,但只有在你真正需要帮助的情况下。 ;) – joran

+0

请把你的问题的信息。这就是为什么你可以编辑你的问题。 – joran

回答

1

eipi10用他的解决方案拯救了一天!我用下面的代码,一切工作正常:

write.xlsx(as.data.frame(provFundedServiceDivision), 
file="output/provFundedSD.xlsx", sheetName="provFundingSD") 

感谢大家的阅读和帮助我。这是我第一个关于堆栈溢出的问题。干杯!

3

使用ungroup()dplyr链的末端:

provFundedServiceDivision <- compensationBase2014 %>% 
    group_by(serviceDivision,provinciallyFunded) %>% 
    summarise(total=sum(fundingRaw)) %>% 
    mutate(percent = paste0(round(100 * total/sum(total),1), "%")) %>% 

    # Add ungroup to the end 
    ungroup() 

#write to second sheet 
write.xlsx(provFundedServiceDivision, file="output/provFundedSD.xlsx", 
      sheetName="provFundingSD") 

而不是...

class(provFundedServiceDivision)[1] 
[1] "grouped_df" 

结果...

class(provFundedServiceDivision)[1] 
[1] "tbl_df" 
相关问题