通过在R

时间:2017-10-24 21:33:29

标签: r dataframe subset

R中的这个问题让我疯狂了一个星期,所以我会对任何帮助感到高兴:我有一个大的数据框架与ca.我希望每年将每只股票分配给10个投资组合中的一个投资组合的100.000估值比率,通过根据我在第二个数据框BP中计算的十分位数创建一个新变量“BM_Portfolio”。

这是1.数据框“CCM2_June_all_stocks”,包含所有估值比率:

日期;股票; BE_ME
2011-06-30; TELENAV INC; 0.38
2012-06-30; TELENAV INC; 0.30
2013-06-30; TELENAV INC; 0.56

这是每年十分位数(=断点)的2.数据帧“BP”:
日期; BE_ME_10%; BE_ME.20%; BE_ME.30%; BE_ME.40%; BE_ME.50%;等
2011-06-30; TELENAV INC; 0.20; 0.29; 0.37; 0.44; 0.53等
2012-06-30; TELENAV INC; 0.22; 0.27; 0.36; 0.45; 0.55等
2013-06-30; TELENAV INC; 0.24; 0.30; 0.34; 0.46; 0.57等

我的代码将每个股票分配给投资组合,但有时投资组合编号错误为-1或+1:

CCM2_June_all_stocks$BM_Portfolio <- ifelse(CCM2_June_all_stocks$Date %in% BP$Date & CCM2_June_all_stocks$BE_ME_Dec <= BP$X10., 1,
                          ifelse(CCM2_June_all_stocks$Date %in% BP$Date & CCM2_June_all_stocks$BE_ME_Dec <= BP$X20., 2,
                          ifelse(CCM2_June_all_stocks$Date %in% BP$Date & CCM2_June_all_stocks$BE_ME_Dec <= BP$X30., 3,
                          ifelse(CCM2_June_all_stocks$Date %in% BP$Date & CCM2_June_all_stocks$BE_ME_Dec <= BP$X40., 4,
                          ifelse(CCM2_June_all_stocks$Date %in% BP$Date & CCM2_June_all_stocks$BE_ME_Dec <= BP$X50., 5,
                          ifelse(CCM2_June_all_stocks$Date %in% BP$Date & CCM2_June_all_stocks$BE_ME_Dec <= BP$X60., 6,
                          ifelse(CCM2_June_all_stocks$Date %in% BP$Date & CCM2_June_all_stocks$BE_ME_Dec <= BP$X70., 7,
                          ifelse(CCM2_June_all_stocks$Date %in% BP$Date & CCM2_June_all_stocks$BE_ME_Dec <= BP$X80., 8,
                          ifelse(CCM2_June_all_stocks$Date %in% BP$Date & CCM2_June_all_stocks$BE_ME_Dec <= BP$X90., 9, 10)))))))))

此外,我收到9条警告消息,因此我假设有更可靠的方法来编写代码:

Warning messages:
1: In CCM2_June_all_stocks$BE_ME_Dec <= BP$X10. :
  longer object length is not a multiple of shorter object length

知道如何解决这个问题吗?解决方案应该是:
日期;股票;做我自己; BM_Portfolio
2011-06-30; TELENAV INC; 0.38; 4 (因为它小于0.44)
2012-06-30; TELENAV INC; 0.30; 3 (因为它小于0.36)
2013-06-30; TELENAV INC; 0.56; 5 (因为它小于0.57)

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

你可能想要这样的东西:

BM_Merged <- merge(CCM2_June_all_stocks, BP, by=c("Date", "stock"))
Portfolio <- apply(BM_Merged, 1, function(x)match(TRUE, x[3] <= x[-(1:3)]))
BM_Result <- cbind(BM_Merged[, 1:3], BM_Portfolio=Portfolio)

如果你必须创建一些笨拙的重复结构,就像你的ifelses一样,那么有一个简单的方法可以做到这一点。