2016-08-31 41 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" 
相關問題