本文介绍了根据xts对象中的信息将数据帧分割成多个数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我真的需要您在以下问题上的帮助: 我有两个数据帧 - 一个包含ISIN和群集信息的证券组合。 > dfInput TICKER CLUSTER SECURITY.NAME 1 LU0937588209高收益Prime Capital Access SA SICAV- 2 LU0694362343高收益ECM信用基金SICAV - ECM Hi 3 IE0030390896高收益Putnam世界信托 - 全球Hi 4 LU0575374342 EM债务Ashmore SICAV - 新兴市场 5 LU0493865678 EM债务Ashmore SICAV - 新兴市场 6 LU0972237696 EM债务Galloway全球固定收益F 7 IE00B6TLWG59 ILS / CAT GAM Star Fund PLC - 猫债券F 8 LU0816333396 ILS / CAT LGT Lux I - 猫债券基金 9 LU0879473352 L / S信用美林证券投资索赔 10 HINCFEF ID权益L / S信用对冲投资国际娱乐 11 FR0011034800 L / S信用Schelcher Prince机会E 12 PIMCSEI ID权益L / S信贷PIMCO基金全球投资者S 13 VTR美国股票REITs Ventas Inc 14 HCP美国股票REITs HCP Inc 15 VGSIX US股权REITs先锋REIT指数基金 16 NLY美国股票M REITs Annaly资本管理公司 17 CLNY美国股票M REITs殖民地金融公司 18 AGNC美国股票M REITs美国首都代理公司 19 REM美国股权M REITs iShares抵押房地产C 20 ES0130960018基础设施资产Enagas SA 21 SDRL美国股权基础设施股权Seadrill Ltd 22 IGF US Equity Infrastructure Equity iShares全球基础设施 23 KMP美国股票MLP金德摩根能源合伙人 24 EPD美国股票MLP企业产品合作伙伴L 25 MLPI美国股票MLP ETRACS Alerian MLP Infrastruct 26 HTGC美国股票BDC大力神技术增长值 27 TCPC美国股票BDC TCP Capital Corp 28主要美元权益BDC主街资本公司 29 BDCS美国股票BDC ETRACS链接到Wells远 另一个包含这些证券的多个时间序列的返回值,其安全名称为列名(数据来自excel文件) > PortfolioR.xts Ventas.Inc HCP.Inc .... 2011-01-03 0.0000000000 0.0000000000 2011-01-04 -0.0117725362 -0.0056323067 2011 -01-05 -0.0081155489 0.0018809625 2011-01-06 -0.0009479572 -0.0154202974 2011-01-07 -0.0058974774 -0.0054674822 2011-01-10 -0.0074691528 -0.0077050464 2011-01-11 -0.0036591278 0.0052348928 2011-01-12 0.0132249172 -0.0091097938 2011-01-13 0.0015220703 0.0085600412 2011-01-14 0.0058762372 -0.0038567541 2011-01 -17 0.0000000000 0.0000000000 2011-01-18 0.0157513101 -0.0002760525 2011-01-19 -0.0059712810 -0.0074823683 2011-01-20 0.0013092679 0.0049944610 2011-01-21 0.0013075560 -0.0055509440 ... 现在我如何拆分基于集群的xts对象资料的信息? 结果应该是为每个 CLUSTER 分开一个 data.frame 或 xts 对象包含属于此群集的证券的回报历史。 这是可能吗? 提前谢谢... 解决方案这里有一种方法: setNames(lapply(dfInput $ CLUSTER),function(x){ PortfolioR [,which(dfInput $ CLUSTER [match(colnames(PortfolioR), dfInput $ SECURITY.NAME)] == x)] }),唯一(dfInput $ CLUSTER)) 例如: #设置一些假数据 d1< - data.frame(grp = sample(LETTERS [1:4],10,replace = TRUE), name = letters [1:10]) d1 #grp name #1 A a #2 B b #3 B c #4 D d #5 C e #6 B f #7 B g #8 A h #9 D i #10 A j d2< - 矩阵(圆(runif(50) ,2),ncol = 10)c olnames(d2) library(xts) d2 as.Date('5-01-2011','%d-%m-%Y'),1)) d2 #abcdefghij #2011-01-01 0.51 0.41 0.69 0.87 0.37 0.86 0.47 0.68 0.64 0.73 #2011-01-02 0.72 0.92 0.53 0.55 0.62 0.54 0.75 0.64 0.04 0.72 #2011-01-03 0.34 0.50 0.92 0.23 0.59 0.09 0.78 0.53 0.26 0.27 #2011-01-04 0.52 0.47 0.49 0.25 0.18 0.07 0.65 0.13 0.46 0.74 #2011-01- 05 0.10 0.87 0.10 0.48 0.58 0.72 0.96 0.71 0.78 0.80 out< - setNames(sapply(unique(d1 $ grp),function(x){ d2 [,which(d1 $ grp [match(colnames(d2),d1 $ name)] == x)] }),unique(d1 $ grp)) out #$ A #ahj #2011-01-01 0.51 0.68 0.73 #2011-01-02 0.72 0.64 0.72 #2011-01-03 0.34 0.53 0.27 #2011-01-04 0.52 0.13 0.74 #2011-01-05 0.10 0.71 0.80 ## $ b#bcfg #2011-01-01 0.41 0.69 0.86 0.47 #2011-01-02 0.92 0.53 0.54 0.75 #2011-01-03 0.50 0.92 0.09 0.78 #2011-01-04 0.47 0.49 0.07 0.65 #2011-01-05 0.87 0.10 0.72 0.96 ## $ C #di #2011-01-01 0.87 0.64 #2011-01-02 0.55 0.04 #2011-01-03 0.23 0.26 #2011 -01-04 0.25 0.46 #2011-01-05 0.48 0.78 ##$ D #e #2011-01-01 0.37 #2011-01-02 0.62 #2011-01-03 0.59 #2011-01-04 0.18 #2011-01-05 0.58 如果您希望列表元素(它们是 xts 对象)独立为code> xts 对象在全球环境中,您可以使用 list2env : list2env(out,globalenv()) 这将覆盖全局环境中与列表元素具有相同名称的任何对象(即。 A,B,C和D)。 I really need your help on the following issue:I have two data frames - one containing a portfolio of securities with ISIN and Cluster information. > dfInput TICKER CLUSTER SECURITY.NAME1 LU0937588209 High Yield Prime Capital Access SA SICAV-2 LU0694362343 High Yield ECM CREDIT FUND SICAV - ECM Hi3 IE0030390896 High Yield Putnam World Trust - Global Hi4 LU0575374342 EM Debt Ashmore SICAV - Emerging Marke5 LU0493865678 EM Debt Ashmore SICAV - Emerging Marke6 LU0972237696 EM Debt Galloway Global Fixed Income F7 IE00B6TLWG59 ILS/CAT GAM Star Fund PLC - Cat Bond F8 LU0816333396 ILS/CAT LGT Lux I - Cat Bond Fund9 LU0879473352 L/S Credit Merrill Lynch Investment Solut10 HINCFEF ID Equity L/S Credit Hedge Invest International Fun11 FR0011034800 L/S Credit Schelcher Prince Opportunite E12 PIMCSEI ID Equity L/S Credit PIMCO Funds Global Investors S13 VTR US Equity REITs Ventas Inc14 HCP US Equity REITs HCP Inc15 VGSIX US Equity REITs Vanguard REIT Index Fund16 NLY US Equity M REITs Annaly Capital Management Inc17 CLNY US Equity M REITs Colony Financial Inc18 AGNC US Equity M REITs American Capital Agency Corp19 REM US Equity M REITs iShares Mortgage Real Estate C20 ES0130960018 Infrastructure Equities Enagas SA21 SDRL US Equity Infrastructure Equities Seadrill Ltd22 IGF US Equity Infrastructure Equities iShares Global Infrastructure23 KMP US Equity MLP Kinder Morgan Energy Partners24 EPD US Equity MLP Enterprise Products Partners L25 MLPI US Equity MLP ETRACS Alerian MLP Infrastruct26 HTGC US Equity BDC Hercules Technology Growth Cap27 TCPC US Equity BDC TCP Capital Corp28 MAIN US Equity BDC Main Street Capital Corp29 BDCS US Equity BDC ETRACS Linked to the Wells FarThe other contains multiple time series of returns of these securities with the security name as column name (the data is coming from an excel file)> PortfolioR.xts Ventas.Inc HCP.Inc ....2011-01-03 0.0000000000 0.00000000002011-01-04 -0.0117725362 -0.00563230672011-01-05 -0.0081155489 0.00188096252011-01-06 -0.0009479572 -0.01542029742011-01-07 -0.0058974774 -0.00546748222011-01-10 -0.0074691528 -0.00770504642011-01-11 -0.0036591278 0.00523489282011-01-12 0.0132249172 -0.00910979382011-01-13 0.0015220703 0.00856004122011-01-14 0.0058762372 -0.00385675412011-01-17 0.0000000000 0.00000000002011-01-18 0.0157513101 -0.00027605252011-01-19 -0.0059712810 -0.00748236832011-01-20 0.0013092679 0.00499446102011-01-21 0.0013075560 -0.0055509440...How can I split now the xts object based on the cluster information of the portfolio?The result should be to have for each CLUSTER a separate data.frame or xts object containing the return history of the securities belonging to this cluster.Is this possible?Thank you in advance... 解决方案 Here's one way to do it:setNames(lapply(unique(dfInput$CLUSTER), function(x) { PortfolioR[, which(dfInput$CLUSTER[match(colnames(PortfolioR), dfInput$SECURITY.NAME)] == x)]}), unique(dfInput$CLUSTER))For example:# Set up some fake datad1 <- data.frame(grp=sample(LETTERS[1:4], 10, replace=TRUE), name=letters[1:10])d1# grp name# 1 A a# 2 B b# 3 B c# 4 D d# 5 C e# 6 B f# 7 B g# 8 A h# 9 D i# 10 A jd2 <- matrix(round(runif(50), 2), ncol=10)colnames(d2) <- letters[1:10]library(xts)d2 <- xts(d2, seq.Date(as.Date('01-01-2011', '%d-%m-%Y'), as.Date('5-01-2011', '%d-%m-%Y'), 1))d2# a b c d e f g h i j# 2011-01-01 0.51 0.41 0.69 0.87 0.37 0.86 0.47 0.68 0.64 0.73# 2011-01-02 0.72 0.92 0.53 0.55 0.62 0.54 0.75 0.64 0.04 0.72# 2011-01-03 0.34 0.50 0.92 0.23 0.59 0.09 0.78 0.53 0.26 0.27# 2011-01-04 0.52 0.47 0.49 0.25 0.18 0.07 0.65 0.13 0.46 0.74# 2011-01-05 0.10 0.87 0.10 0.48 0.58 0.72 0.96 0.71 0.78 0.80out <- setNames(sapply(unique(d1$grp), function(x) { d2[, which(d1$grp[match(colnames(d2), d1$name)] == x)]}), unique(d1$grp))out# $A# a h j# 2011-01-01 0.51 0.68 0.73# 2011-01-02 0.72 0.64 0.72# 2011-01-03 0.34 0.53 0.27# 2011-01-04 0.52 0.13 0.74# 2011-01-05 0.10 0.71 0.80## $B# b c f g# 2011-01-01 0.41 0.69 0.86 0.47# 2011-01-02 0.92 0.53 0.54 0.75# 2011-01-03 0.50 0.92 0.09 0.78# 2011-01-04 0.47 0.49 0.07 0.65# 2011-01-05 0.87 0.10 0.72 0.96## $C# d i# 2011-01-01 0.87 0.64# 2011-01-02 0.55 0.04# 2011-01-03 0.23 0.26# 2011-01-04 0.25 0.46# 2011-01-05 0.48 0.78## $D# e# 2011-01-01 0.37# 2011-01-02 0.62# 2011-01-03 0.59# 2011-01-04 0.18# 2011-01-05 0.58If you want the list elements (which are xts objects) to be standalone xts objects in the global environment, you can use list2env:list2env(out, globalenv())This will overwrite any objects in the global environment that have the same names as the list elements (i.e. A, B, C and D for the example above). 这篇关于根据xts对象中的信息将数据帧分割成多个数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-11 17:44