2017-05-04 74 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

相关问题