2016-08-31 3 views
2

dplyr을 사용하여 xlsx를 사용하여 스프레드 시트에 쓰는 개체를 만듭니다.xlsx 패키지를 사용하여 Excel 파일을 쓸 수 없습니다. R

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") 

는 다음 코드를 실행합니다. 여기 미친려고하고

Error: cannot convert object to a data frame 

:

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") 

나에게 다음과 같은 오류를 제공합니다 :

나는 다음 수준 추락 다음 코드를 실행합니다. 도대체 무슨 일이 일어나는 지 아는 사람이 있습니까? 여러 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)의 출력을 제공 할 수 있습니다. 부통령)'. 결과를 얻은 후에'traceback()'의 결과 일 수도 있지만 _ 도움이 필요할 경우에만 가능합니다. ;) – joran

+0

질문에 정보를 입력하십시오. 그렇기 때문에 질문을 편집 할 수 있습니다. – joran

답변

1

eipi10은 자신의 솔루션으로 그날을 저장했습니다! 다음 코드를 사용했는데 모든 것이 올바르게 작동했습니다.

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

감사하고 나를 도와 주신 모든 분들께 감사드립니다. 이것은 스택 오버 플로우에 대한 나의 첫 번째 질문이다. 건배! 당신의 dplyr 체인의 끝에

3

사용 ungroup() :

대신
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" 
관련 문제