如何将我的数据框架转换为XTS对象?每个人都应具有姓名位于第一列的投资组合的日期和返回

发布于 2025-01-22 21:07:51 字数 211 浏览 2 评论 0原文

我从包含一年中许多不同投资组合的每日申报表的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟沫凡尘 2025-01-29 21:07:52

要创建XTS对象,请首先在数据框中转换目标列(在您的情况下,active_returns)为矩阵,然后将date> ditats列设置为行矩阵的名称。然后,使用as.xts函数将矩阵转换为XTS对象。

您不提供任何数据框架,因此这是一个:

set.seed(1)
dates <- seq(as.Date("2020-01-01"), as.Date("2020-01-10"), by = "days")
portfolios <- sample(LETTERS[1:3], size = 10, replace = TRUE )
returns <- runif(10, min = 10, max = 100)
dat <- data.frame(dates, portfolios, returns)
dat
#        dates  portfolios  returns
# 1  2020-01-01          A 25.89011
# 2  2020-01-02          C 71.83206
# 3  2020-01-03          A 44.56933
# 4  2020-01-04          B 79.28573
# 5  2020-01-05          A 54.79293
# 6  2020-01-06          C 74.58567
# 7  2020-01-07          C 99.27155
# 8  2020-01-08          B 44.20317
# 9  2020-01-09          B 79.97007
# 10 2020-01-10          C 94.12347

将步骤包装在以数据框为参数的函数中包裹步骤是有效的。

library(xts)
make_xts <- function(yourdf){
  dat_matrix <- as.matrix(yourdf["returns"])
  rownames(dat_matrix) <- unlist(as.character.Date(yourdf["dates"]))
  return(as.xts(dat_matrix))
}

然后,可以将此功能应用于每个组的目标数据框架。例如,组变量为portfolios

base-r中,您可以使用 :

xts_list <- by(dat, dat$portfolios, make_xts)
xts_list
# dat$portfolios: A
# returns
# 2020-01-01 25.89011
# 2020-01-03 44.56933
# 2020-01-05 54.79293
# ------------------------------------------------------------ 
#   dat$portfolios: B
# returns
# 2020-01-04 79.28573
# 2020-01-08 44.20317
# 2020-01-09 79.97007
# ------------------------------------------------------------ 
#   dat$portfolios: C
# returns
# 2020-01-02 71.83206
# 2020-01-06 74.58567
# 2020-01-07 99.27155
# 2020-01-10 94.12347

结果是一个列表,因此您可以通过使用$ :

xts_list$A
            returns
2020-01-01 25.89011
2020-01-03 44.56933
2020-01-05 54.79293

检查它是否真的是XTS对象:

str(xts_list$A)
# An ‘xts’ object on 2020-01-01/2020-01-05 containing:
#   Data: num [1:3, 1] 25.9 44.6 54.8
# - attr(*, "dimnames")=List of 2
# ..$ : NULL
# ..$ : chr "returns"
# Indexed by objects of class: [POSIXct,POSIXt] TZ: 
#   xts Attributes:  
#   NULL

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 the dates column as the row names of the matrix. Then, you convert the matrix to an xts object using as.xts function.

You don't provide any data frame as an example, so here is one:

set.seed(1)
dates <- seq(as.Date("2020-01-01"), as.Date("2020-01-10"), by = "days")
portfolios <- sample(LETTERS[1:3], size = 10, replace = TRUE )
returns <- runif(10, min = 10, max = 100)
dat <- data.frame(dates, portfolios, returns)
dat
#        dates  portfolios  returns
# 1  2020-01-01          A 25.89011
# 2  2020-01-02          C 71.83206
# 3  2020-01-03          A 44.56933
# 4  2020-01-04          B 79.28573
# 5  2020-01-05          A 54.79293
# 6  2020-01-06          C 74.58567
# 7  2020-01-07          C 99.27155
# 8  2020-01-08          B 44.20317
# 9  2020-01-09          B 79.97007
# 10 2020-01-10          C 94.12347

It's efficient to wrap the steps in a function that takes a data frame as its argument.

library(xts)
make_xts <- function(yourdf){
  dat_matrix <- as.matrix(yourdf["returns"])
  rownames(dat_matrix) <- unlist(as.character.Date(yourdf["dates"]))
  return(as.xts(dat_matrix))
}

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:

xts_list <- by(dat, dat$portfolios, make_xts)
xts_list
# dat$portfolios: A
# returns
# 2020-01-01 25.89011
# 2020-01-03 44.56933
# 2020-01-05 54.79293
# ------------------------------------------------------------ 
#   dat$portfolios: B
# returns
# 2020-01-04 79.28573
# 2020-01-08 44.20317
# 2020-01-09 79.97007
# ------------------------------------------------------------ 
#   dat$portfolios: C
# returns
# 2020-01-02 71.83206
# 2020-01-06 74.58567
# 2020-01-07 99.27155
# 2020-01-10 94.12347

The result is a list, so you can subset any element of it by using $:

xts_list$A
            returns
2020-01-01 25.89011
2020-01-03 44.56933
2020-01-05 54.79293

To check if it is really an xts object:

str(xts_list$A)
# An ‘xts’ object on 2020-01-01/2020-01-05 containing:
#   Data: num [1:3, 1] 25.9 44.6 54.8
# - attr(*, "dimnames")=List of 2
# ..$ : NULL
# ..$ : chr "returns"
# Indexed by objects of class: [POSIXct,POSIXt] TZ: 
#   xts Attributes:  
#   NULL
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文