R 根据xts对象中的信息将数据帧拆分为多个数据帧第2部分

R 根据xts对象中的信息将数据帧拆分为多个数据帧第2部分,r,split,dataframe,xts,R,Split,Dataframe,Xts,以我的第一个问题为出发点: 现在我有一个新问题 假设您的data.frames d1和d2中有两个条目,即d1中有grp B和A中的字母“h”,因此d2中也有两个时间序列中的“A”。我们如何解决这个问题 d1 <- data.frame(grp=sample(LETTERS[1:4], 11, replace=TRUE), name=letters[c(8,1:10)]) > d1 grp name 1 B h 2 D a

以我的第一个问题为出发点:

现在我有一个新问题

假设您的data.frames d1和d2中有两个条目,即d1中有grp B和A中的字母“h”,因此d2中也有两个时间序列中的“A”。我们如何解决这个问题

d1 <- data.frame(grp=sample(LETTERS[1:4], 11, replace=TRUE),
                 name=letters[c(8,1:10)])
> d1
grp name
1    B    h
2    D    a
3    B    b
4    D    c
5    B    d
6    C    e
7    A    f
8    A    g
9    A    h
10   B    i
11   C    j

d2 <- matrix(round(runif(55), 2), ncol=11)
colnames(d2) <- letters[c(8,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
              h    a    b    c    d    e    f    g    h    i    j
2011-01-01 0.04 0.77 0.49 0.87 0.23 0.95 0.69 0.35 0.14 0.47 0.25
2011-01-02 0.73 0.46 0.28 0.86 0.75 0.08 0.00 0.89 0.50 0.12 0.54
2011-01-03 0.36 0.61 0.92 0.80 0.12 0.25 0.18 0.44 0.73 0.19 0.30
2011-01-04 0.18 0.65 0.68 0.44 0.54 0.84 0.13 0.64 0.54 0.81 0.73
2011-01-05 0.58 0.55 0.10 0.33 0.55 0.23 0.82 0.21 0.58 0.24 0.04
非常感谢您的帮助

提前感谢您……

您可以:

  name1 <- with(d1,paste0(name,"#", ave(name, name,
                                    FUN=seq_along)))
  colnames(d2) <- name1 #assuming that it is in the same order as in the example

  res <- lapply(split(name1, d1$grp), function(x) {
                 x1 <- d2[, as.character(x), drop=FALSE]
                 colnames(x1) <- gsub("\\#.*$", '', colnames(x1))
                 x1})

  res1 <- res[match(unique(d1$grp), names(res))]

  res1
  # $B
  #            h    b    d    i
  #2011-01-01 0.04 0.49 0.23 0.47
  #2011-01-02 0.73 0.28 0.75 0.12
  #2011-01-03 0.36 0.92 0.12 0.19
  #2011-01-04 0.18 0.68 0.54 0.81
  #2011-01-05 0.58 0.10 0.55 0.24

  # $D
  #            a    c
  #2011-01-01 0.77 0.87
  #2011-01-02 0.46 0.86
  #2011-01-03 0.61 0.80
  #2011-01-04 0.65 0.44
  #2011-01-05 0.55 0.33

  # $C
  #            e    j
  #2011-01-01 0.95 0.25
  #2011-01-02 0.08 0.54
  #2011-01-03 0.25 0.30
  #2011-01-04 0.84 0.73
  #2011-01-05 0.23 0.04

  # $A
  #            f    g    h
  #2011-01-01 0.69 0.35 0.14
  #2011-01-02 0.00 0.89 0.50
  #2011-01-03 0.18 0.44 0.73
  #2011-01-04 0.13 0.64 0.54
  #2011-01-05 0.82 0.21 0.58

name1你好,我已经更新了我的问题,显示了预期的输出。看看$A组。在这里你可以看到第二个时间序列h。谢谢你的时间!它是否必须按特定的顺序排列,即列表元素?嗨,akrun,不幸的是,顺序很重要,因为假设你对h有不同的权重。假设grp B中的重量为0.3,grp a中的重量为0.5。谢谢如果我理解正确,第一列
h
应该根据它们在
d1
中出现的顺序进入
B
,第二列进入
A
这是正确的。感谢您,在您的示例中,您可以看到h在$A和$B中的时间序列是相同的。仔细观察d2,可以得到h的不同时间序列。Thanks@Daniel_D谢谢,我没有检查。非常感谢!!!很好用!只有一个缺点。函数将删除colnames中的所有数字。我的真实数据包含证券名称,即名称中有到期日的债券。是否可以只删除最后一个号码?这个想法是伟大的序列在最后。非常感谢。我们很接近。假设我的证券名称是:2016年12月30日300万欧元欧元。按照顺序,您将在161年12月获得300万欧元的欧元银行同业拆借利率。函数后的名称是300万欧元欧元欧元,这对您来说很复杂。我同意。你真是个天才!!
out

$B
              h    b    d    i
2011-01-01 0.04 0.49 0.23 0.47
2011-01-02 0.73 0.28 0.75 0.12
2011-01-03 0.36 0.92 0.12 0.19
2011-01-04 0.18 0.68 0.54 0.81
2011-01-05 0.58 0.10 0.55 0.24

$D
              a    c
2011-01-01 0.77 0.87
2011-01-02 0.46 0.86
2011-01-03 0.61 0.80
2011-01-04 0.65 0.44
2011-01-05 0.55 0.33

$C
              e    j
2011-01-01 0.95 0.25
2011-01-02 0.08 0.54
2011-01-03 0.25 0.30
2011-01-04 0.84 0.73
2011-01-05 0.23 0.04

$A
              f    g    h
2011-01-01 0.69 0.35 0.14
2011-01-02 0.00 0.89 0.50
2011-01-03 0.18 0.44 0.73
2011-01-04 0.13 0.64 0.54
2011-01-05 0.82 0.21 0.58
  name1 <- with(d1,paste0(name,"#", ave(name, name,
                                    FUN=seq_along)))
  colnames(d2) <- name1 #assuming that it is in the same order as in the example

  res <- lapply(split(name1, d1$grp), function(x) {
                 x1 <- d2[, as.character(x), drop=FALSE]
                 colnames(x1) <- gsub("\\#.*$", '', colnames(x1))
                 x1})

  res1 <- res[match(unique(d1$grp), names(res))]

  res1
  # $B
  #            h    b    d    i
  #2011-01-01 0.04 0.49 0.23 0.47
  #2011-01-02 0.73 0.28 0.75 0.12
  #2011-01-03 0.36 0.92 0.12 0.19
  #2011-01-04 0.18 0.68 0.54 0.81
  #2011-01-05 0.58 0.10 0.55 0.24

  # $D
  #            a    c
  #2011-01-01 0.77 0.87
  #2011-01-02 0.46 0.86
  #2011-01-03 0.61 0.80
  #2011-01-04 0.65 0.44
  #2011-01-05 0.55 0.33

  # $C
  #            e    j
  #2011-01-01 0.95 0.25
  #2011-01-02 0.08 0.54
  #2011-01-03 0.25 0.30
  #2011-01-04 0.84 0.73
  #2011-01-05 0.23 0.04

  # $A
  #            f    g    h
  #2011-01-01 0.69 0.35 0.14
  #2011-01-02 0.00 0.89 0.50
  #2011-01-03 0.18 0.44 0.73
  #2011-01-04 0.13 0.64 0.54
  #2011-01-05 0.82 0.21 0.58
  d1  <- structure(list(grp = c("B", "D", "B", "D", "B", "C", "A", "A", 
  "A", "B", "C"), name = c("h", "a", "b", "c", "d", "e", "f", "g", 
  "h", "i", "j")), .Names = c("grp", "name"), class = "data.frame", row.names = 
  c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"))


  d2 <- structure(c(0.04, 0.73, 0.36, 0.18, 0.58, 0.77, 0.46, 0.61, 0.65, 
  0.55, 0.49, 0.28, 0.92, 0.68, 0.1, 0.87, 0.86, 0.8, 0.44, 0.33, 
  0.23, 0.75, 0.12, 0.54, 0.55, 0.95, 0.08, 0.25, 0.84, 0.23, 0.69, 
  0, 0.18, 0.13, 0.82, 0.35, 0.89, 0.44, 0.64, 0.21, 0.14, 0.5, 
  0.73, 0.54, 0.58, 0.47, 0.12, 0.19, 0.81, 0.24, 0.25, 0.54, 0.3, 
  0.73, 0.04), .Dim = c(5L, 11L), .Dimnames = list(NULL, c("h", 
  "a", "b", "c", "d", "e", "f", "g", "h", "i", "j")), index = 
  structure(c(1293840000, 1293926400, 1294012800, 1294099200, 1294185600), 
  tzone = "UTC", tclass = "Date"), .indexCLASS = "Date", tclass = "Date",
   .indexTZ = "UTC", tzone = "UTC", class = c("xts", "zoo"))