2017-05-04 57 views
1

我有兩個輸入數據框,第一個被稱爲「Firms_Ind」,包含2列(「公司」,「行業」),具有多行。它給出了每家公司的行業ID。另一個稱爲「ann_returns」,其具有與「Firms_Ind」具有多行並具有多行的列數一樣多的列。它包含每年(行)每個公司(列)的回報。根據另一個向量中的ID行列總結

我想計算每個行業的年均回報。所以我想要一個輸出矩陣,它的維度是:列數=年數和行數=年數。對於每個行業(專欄),應計算每年的平均回報。

這裏是一個小例子:

> Firms_Ind 
    Firms Industry 
1  A  1 
2  B  2 
3  C  3 
4  D  1 
5  E  2 
6  F  1 

> ann_returns 
     A B C D E F 
y1 0.20 0.11 0.13 0.30 0.24 0.03 
y2 0.23 0.08 0.03 0.23 0.17 0.01 
y3 0.28 0.19 0.11 0.21 0.19 0.07 

> Industry_mean 
      1 2 3 
y1_means 0.20 0.11 0.13 
y2_means 0.23 0.08 0.03 
y3_means 0.28 0.19 0.11 
+0

你嘗試重塑'ann_returns'爲長格式,然後合併'Firms_Ind'它,再由行業組織來計算是什麼意思? – coffeinjunky

回答

1

下面是一個方法與sapply

# get a list of firms by industry 
inds <- split(Firms_Ind$Firms, Firms_Ind$Industry) 
# loop through industries to calculate annual means 
myMat <- sapply(inds, 
       function(i) if(length(i) > 1) rowMeans(ann_returns[, i]) else ann_returns[, i]) 

這裏,sapply循環通過行業。對於每個行業,檢查是否有多個公司,如果是,則應用rowMeans,如果不是,則返回原始值。

這將返回

myMat 
      1  2 3 
y1 0.1766667 0.175 0.13 
y2 0.1566667 0.125 0.03 
y3 0.1866667 0.190 0.11 

數據

Firms_Ind <- 
structure(list(Firms = structure(1:6, .Label = c("A", "B", "C", 
"D", "E", "F"), class = "factor"), Industry = c(1L, 2L, 3L, 1L, 
2L, 1L)), .Names = c("Firms", "Industry"), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6")) 

ann_returns <- 
structure(c(0.2, 0.23, 0.28, 0.11, 0.08, 0.19, 0.13, 0.03, 0.11, 
0.3, 0.23, 0.21, 0.24, 0.17, 0.19, 0.03, 0.01, 0.07), .Dim = c(3L, 
6L), .Dimnames = list(c("y1", "y2", "y3"), c("A", "B", "C", "D", 
"E", "F"))) 
+0

感謝您的支持!這似乎是我正在尋找的解決方案。但是,如果我用我的實際數據幀運行它,會發生以下錯誤: 錯誤[[.data.frame](ROE_ac,,i):未定義的列被選中 我的數據框:ann_returns是ROE_ac(R = 305,C = 2)和Firms_Ind是firm_FF(R = 30,C = 305) – Tobi1990

1

使用dplyrtidyr

library(tidyr) 
library(dplyr) 

Industry_mean <- ann_returns %>% 
     gather(key=Firms,value=value,-Year) %>% #convert to long format 
     left_join(Firms_Ind) %>% #merge with firms_ind 
     group_by(Year,Industry) %>% #group as required 
     summarise(mean=mean(value)) %>% #calculate means 
     spread(key=Industry,value=mean) #convert back to wide format 

Industry_mean 

    Year  `1` `2` `3` 
* <chr>  <dbl> <dbl> <dbl> 
1 y1 0.1766667 0.175 0.13 
2 y2 0.1566667 0.125 0.03 
3 y3 0.1866667 0.190 0.11 
1

我們可以通過列拆分ann_returns,然後運行rowMeans

# if Firms in correct order 
inds <- split.default(ann_returns, f = Firms_Ind$Industry) 

# # if Firms not in correct order: 
# inds <- split.default(
#  ann_returns, 
#  f = Firms_Ind$Industry[match(colnames(ann_returns), Firms_Ind$Firms)]) 

do.call(cbind, lapply(inds,rowMeans)) 
#   1  2 3 
# y1 0.1766667 0.175 0.13 
# y2 0.1566667 0.125 0.03 
# y3 0.1866667 0.190 0.11 

兩個輸入data.frames是:

# > dput(ann_returns) 
structure(list(A = c(0.2, 0.23, 0.28), B = c(0.11, 0.08, 0.19 
), C = c(0.13, 0.03, 0.11), D = c(0.3, 0.23, 0.21), E = c(0.24, 
0.17, 0.19), F = c(0.03, 0.01, 0.07)), .Names = c("A", "B", "C", 
"D", "E", "F"), row.names = c("y1", "y2", "y3"), class = "data.frame") 
# > dput(Firms_Ind) 
structure(list(Firms = structure(1:6, .Label = c("A", "B", "C", 
"D", "E", "F"), class = "factor"), Industry = c(1L, 2L, 3L, 1L, 
2L, 1L)), .Names = c("Firms", "Industry"), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6")) 
+0

謝謝!如果公司的順序正確,你究竟是什麼意思? – Tobi1990

+0

@ Tobi1990,我的意思是'ann_returns'的列名與Firms_Ind的Firm列的順序是否相同,這樣你就可以直接拆分而不需要先匹配Firm的名字。 – mt1022

+0

是的。再次感謝,您的解決方案完美無缺! – Tobi1990

相關問題