2011-07-12 66 views
12

我正在撰寫一份報告,要求在Excel中生成許多數據透視表。我想認爲有一種方法可以在R中執行此操作,以便我可以避免使用Excel。我想輸出像下面的截圖(教師姓名編輯)。據我所知,我可以使用重塑包來計算聚合值,但我需要做很多次,並以某種方式以正確的順序獲取所有數據。那時,我應該在Excel中做這件事。有沒有人有任何建議或包裝建議?謝謝!R中的數據透視表式輸出?

(EDIT) 數據開始爲學生名單,他們的老師,學校和成長。然後將這些數據進行彙總,以獲得教師與其平均班級增長的列表。請注意,教師然後按學校分組。我預見到R最大的問題是如何獲得小計和總計行數(BSA1 Total,Grand Total等),因爲它們與其他類型的觀測不同。你只是手動計算它們並試圖讓它們按正確的順序排列,以便它們出現在該組的底部?

example

+0

因爲目前的情況是不明確,合理的網站就在這裏,我認爲回答這個問題。我只能說:學習乳膠,如果你不知道這一點,看看這個問題:http://stackoverflow.com/questions/5465314/tools-for-making-latex-tables-in-r並獲取數據幀使用例如'plyr'或'reshape'的正確格式或者像'cast'這樣的基本函數等等。獲得你輸出的內容在R中很容易,但是你會以文本格式得到它。佈局應該在其他地方完成。 –

+0

你應該看看R數據幀。 – ATMathew

+1

當然在R中有很多簡單的方法可以做到這一點,但如果你可以提供什麼數據開始,那麼除了你想要的結果之外,你會得到更好更適用的代碼會更好。 – John

回答

19

這裏是在計算位的贓物:

set.seed(1) 
school <- sample(c("BSA1", "BSA2", "HSA1"), 100, replace=T) 
teacher <- sample(c("Tom", "Dick", "Harry"), 100, replace=T) 
growth <- rnorm(100, 5, 3) 

myDf <- data.frame(school, teacher, growth) 

require(reshape2) 

aggregate(growth ~ school + teacher, data =myDf, FUN=mean) 

myDf.melt <- melt(myDf, measured="growth") 
dcast(myDf.melt, school + teacher ~ ., fun.aggregate=mean, margins=c("school", "teacher")) 

我還沒有解決的輸出格式,只計算。生成的數據幀應如下所示:

school teacher  NA 
1 BSA1 Dick 4.663140 
2 BSA1 Harry 4.310802 
3 BSA1  Tom 5.505247 
4 BSA1 (all) 4.670451 
5 BSA2 Dick 6.110988 
6 BSA2 Harry 5.007221 
7 BSA2  Tom 4.337063 
8 BSA2 (all) 5.196018 
9 HSA1 Dick 4.508610 
10 HSA1 Harry 4.890741 
11 HSA1  Tom 4.721124 
12 HSA1 (all) 4.717335 
13 (all) (all) 4.886576 

該示例使用reshape2包處理小計。

我認爲R是這裏的工作的工具。我完全可以理解,不確定如何開始分析。幾年前,我從Excel來到R,開始時可能很難。讓我指出了四次親技巧,幫助你在堆棧溢出更好的答案:

1)提供的數據,即使模擬:你可以看到我在回答的開頭模擬一些數據。如果你已經提供了這個模擬,它會a)爲我節省時間b)讓你得到一個使用你自己的數據結構的答案,而不是我夢寐以求的答案,以及c)其他人會回答的答案。我經常跳過沒有數據的問題,因爲我厭倦了猜測他們被告知我的答案的數據,因爲我猜錯了。

2)提出一個清晰的問題。 「我怎麼做我的工作」並不是一個明確的問題。 「如何將此示例數據作爲示例輸出並在聚合中創建小計」是一個具體問題。

3)不停的問!我們都練習得更好。你試圖在R中做更多的事情,而在Excel中做得更少,這樣你的情報就顯然高於平均水平。繼續使用R並不斷提問。這一切都會變得更容易。

4)當你描述的東西要小心你的話。你在編輯的問題中說你有一個「列表」的東西。 R中的列表是特定的數據結構。我懷疑你實際上有一個數據框,並且在一般意義上使用術語「列表」。這可能會造成一些混淆。它也說明了你爲什麼要提供自己的數據。

+1

中查看解決方案會很有趣謝謝!非常有幫助的答案。 –

+2

如果你之後要處理格式化,你會推薦使用xtable和Sweave嗎?或者其他你推薦的東西?再次感謝你。 –

10

使用JD長的模擬數據,並增加了SD和計數:

library(reshape) # not reshape2 
    cast(myDf.melt, school + teacher ~ ., margins=TRUE , c(mean, sd, length)) 
    school teacher  mean  sd length 
1 BSA1 Dick 4.663140 3.718773  14 
2 BSA1 Harry 4.310802 1.430594  9 
3 BSA1  Tom 5.505247 4.045846  4 
4 BSA1 (all) 4.670451 3.095980  27 
5 BSA2 Dick 6.110988 2.304104  15 
6 BSA2 Harry 5.007221 2.908146  9 
7 BSA2  Tom 4.337063 2.789244  14 
8 BSA2 (all) 5.196018 2.682924  38 
9 HSA1 Dick 4.508610 2.946961  11 
10 HSA1 Harry 4.890741 2.977305  13 
11 HSA1  Tom 4.721124 3.193576  11 
12 HSA1 (all) 4.717335 2.950959  35 
13 (all) (all) 4.886576 2.873637 100 
+0

好的例證。我很懶,只是意味着:) –

+0

我認爲它應該很容易與dcast :: reshape2,但無法讓它的工作,所以我恢復了cast :: reshape。 –

+0

哦......有趣。我也不能在reshape2中做到這一點。 –

1

下面是使用較新的pivottabler包生成此的幾種不同的方式。

披露:我是軟件包的作者。

欲瞭解更多信息,請參閱CRAN上的包頁面和該頁面上提供的各種包裝短片。

示例數據(同上)

set.seed(1) 
school <- sample(c("BSA1", "BSA2", "HSA1"), 100, replace=T) 
teacher <- sample(c("Tom", "Dick", "Harry"), 100, replace=T) 
growth <- rnorm(100, 5, 3) 
myDf <- data.frame(school, teacher, growth) 

連結樞軸表輸出到控制檯作爲純文本

library(pivottabler) 
# arguments: qhpvt(dataFrame, rows, columns, calculations, ...) 
qpvt(myDf, c("school", "teacher"), NULL, 
    c("Average Growth"="mean(growth)", "Std Dev"="sd(growth)", 
     "# of Scholars"="n()"), 
    formats=list("%.1f", "%.1f", "%.0f")) 

控制檯輸出:

   Average Growth Std Dev # of Scholars 
BSA1 Dick    4.7  3.7    14 
     Harry    4.3  1.4    9 
     Tom    5.5  4.0    4 
     Total    4.7  3.1    27 
BSA2 Dick    6.1  2.3    15 
     Harry    5.0  2.9    9 
     Tom    4.3  2.8    14 
     Total    5.2  2.7    38 
HSA1 Dick    4.5  2.9    11 
     Harry    4.9  3.0    13 
     Tom    4.7  3.2    11 
     Total    4.7  3.0    35 
Total     4.9  2.9   100 

快速樞軸表輸出作爲一個HTML插件

library(pivottabler) 
qhpvt(myDf, c("school", "teacher"), NULL, 
    c("Average Growth"="mean(growth)", "Std Dev"="sd(growth)", 
     "# of Scholars"="n()"), 
    formats=list("%.1f", "%.1f", "%.0f")) 

HTML小組件輸出:

enter image description here

生成透視表使用更詳細的語法

這具有更多的選項,例如重命名總數。

library(pivottabler) 
pt <- PivotTable$new() 
pt$addData(myDf) 
pt$addRowDataGroups("school", totalCaption="(all)") 
pt$addRowDataGroups("teacher", totalCaption="(all)") 
pt$defineCalculation(calculationName="c1", caption="Average Growth", 
    summariseExpression="mean(growth)", format="%.1f") 
pt$defineCalculation(calculationName="c2", caption="Std Dev", 
    summariseExpression="sd(growth)", format="%.1f") 
pt$defineCalculation(calculationName="c3", caption="# of Scholars", 
    summariseExpression="n()", format="%.0f") 
pt # to output to console as plain text 
pt$renderPivot() # to output as a html widget 

HTML小組件輸出:

enter image description here

0

對不起autopromotion但看看我的包expss

代碼產生以下輸出:上述

set.seed(1) 
school <- sample(c("BSA1", "BSA2", "HSA1"), 100, replace=T) 
teacher <- sample(c("Tom", "Dick", "Harry"), 100, replace=T) 
growth <- rnorm(100, 5, 3) 

myDf <- data.frame(school, teacher, growth) 

library(expss) 
myDf %>% 
    # 'tab_cells' - variables on which statistics will be calculated 
    # "|" is needed to suppress 'growth' in row labels 
    tab_cells("|" = growth) %>% 
    # 'tab_cols' - variables for columns. Can be ommited 
    tab_cols(total(label = "")) %>% 
    # 'tab_rows' - variables for rows. 
    tab_rows(school %nest% list(teacher, "(All)"), "|" = "(All)") %>% 
    # 'method = list' is needed for statistics labels in column 
    tab_stat_fun("Average Growth" = mean, 
       "Std Dev" = sd, 
       "# of scholars" = length, 
       method = list) %>% 
    # finalize table 
    tab_pivot() 

代碼給出其可以與標準的R操作(子集與[等)被用於從data.frame繼承的對象。但是這個對象有一個特殊的print方法。控制檯輸出:通過htmlTable

|  |  | Average Growth | Std Dev | # of scholars | 
| ----- | ----- | -------------- | ------- | ------------- | 
| BSA1 | Dick |   4.7 |  3.7 |   14 | 
|  | Harry |   4.3 |  1.4 |    9 | 
|  | Tom |   5.5 |  4.0 |    4 | 
|  | (All) |   4.7 |  3.1 |   27 | 
| BSA2 | Dick |   6.1 |  2.3 |   15 | 
|  | Harry |   5.0 |  2.9 |    9 | 
|  | Tom |   4.3 |  2.8 |   14 | 
|  | (All) |   5.2 |  2.7 |   38 | 
| HSA1 | Dick |   4.5 |  2.9 |   11 | 
|  | Harry |   4.9 |  3.0 |   13 | 
|  | Tom |   4.7 |  3.2 |   11 | 
|  | (All) |   4.7 |  3.0 |   35 | 
| (All) |  |   4.9 |  2.9 |   100 | 

產出knitr,RStudio觀衆或閃亮: