如何将我的数据框架转换为XTS对象?每个人都应具有姓名位于第一列的投资组合的日期和返回
我从包含一年中许多不同投资组合的每日申报表的Excel文件中获取我的信息。我的数据看起来像。我出于法律原因隐藏了回报。 我想为每个投资组合制作一个XTS对象,仅包含日期和返回。请记住,投资组合数量超过200,我不想手动进行。
I obtain my information from an excel file that contains the daily returns of many different portfolios for one year. My data looks like this.I hid the returns for legal reasons.
I want to make one xts object for each portfolio containing only the dates and the returns. Keep in mind that the portfolios more than 200 in number, I do not want to do it manually.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(1)
要创建XTS对象,请首先在数据框中转换目标列(在您的情况下,
active_returns
)为矩阵,然后将date> ditats
列设置为行矩阵的名称。然后,使用as.xts
函数将矩阵转换为XTS对象。您不提供任何数据框架,因此这是一个:
将步骤包装在以数据框为参数的函数中包裹步骤是有效的。
然后,可以将此功能应用于每个组的目标数据框架。例如,组变量为
portfolios
base-r中,您可以使用 :
结果是一个列表,因此您可以通过使用
$ :
检查它是否真的是XTS对象:
To create an xts object, first convert your targeted column in your data frame (in your case, the
active_returns
) to a matrix, and then set thedates
column as the row names of the matrix. Then, you convert the matrix to an xts object usingas.xts
function.You don't provide any data frame as an example, so here is one:
It's efficient to wrap the steps in a function that takes a data frame as its argument.
Then, this function can be applied to the targeted data frame for each group. For example, the group variable is
portfolios
In base-R, you can use
by
:The result is a list, so you can subset any element of it by using
$
:To check if it is really an xts object: