【问题标题】:Making a matrix out of a list with certain repeating rows and columns in R从列表中制作一个矩阵,在 R 中具有某些重复的行和列
【发布时间】:2020-09-29 01:58:49
【问题描述】:

我有一个在 R 中看起来像这样的数据列表:

enter image description here

在整个列表中,“Tenor”列重复相同的 11 个元素,“last update”列每 11 个元素更改一次。 我想制作一个矩阵,其中 Tenors 作为列名,最后更新日期作为行名。该矩阵应该是具有相应投标收益率的字段。 我不知道如何创建这样一个矩阵,它将相应的投标收益率放入矩阵中。

dput(我的数据)是这样的 “2Y”、“3Y”、“4Y”、“5Y”、“7Y”、“10Y”、“15Y”、“20Y”、“25Y”、“30Y”、 “1Y”、“2Y”、“3Y”、“4Y”、“5Y”、“7Y”、“10Y”、“15Y”、“20Y”、“25Y”、 “30Y”、“1Y”、“2Y”、“3Y”、“4Y”、“5Y”、“7Y”、“10Y”、“15Y”、“20Y”、 “25Y”、“30Y”、“1Y”、“2Y”、“3Y”、“4Y”、“5Y”、“7Y”、“10Y”、“15Y”、 “20Y”、“25Y”、“30Y”、“1Y”、“2Y”、“3Y”、“4Y”、“5Y”、“7Y”、“10Y”、 “15Y”、“20Y”、“25Y”、“30Y”、“1Y”、“2Y”、“3Y”、“4Y”、“5Y”、“7Y”、 "10Y", "15Y", "20Y", "25Y", "30Y")), .Names = c("Bid Yield", "最后更新", "Tenor"), row.names = c(NA, -25256L), class= "data.frame")

【问题讨论】:

    标签: r list matrix


    【解决方案1】:

    我们可以使用xtabs

    xtabs(BidYield ~ LastUpdate + Tenor, df1)
    #         Tenor
    #LastUpdate     10Y   15Y    1Y   20Y   25Y    2Y   30Y    3Y    4Y    5Y    7Y
    #  2011-04-15 4.807 5.233 0.666 5.411 5.504 1.315 5.504 2.105 2.780 3.355 4.180
    #  2011-04-18 4.785 5.206 0.653 5.395 5.491 1.280 5.486 2.053 2.727 3.311 4.142
    

    如果列名有空格等,请在列名周围使用反引号

    names(df1)[1:2] <- c("Bid Yield", "Last Update")
    xtabs(`Bid Yield` ~ `Last Update` + Tenor, df1)
    #   Tenor
    #Last Update    10Y   15Y    1Y   20Y   25Y    2Y   30Y    3Y    4Y    5Y    7Y
    #  2011-04-15 4.807 5.233 0.666 5.411 5.504 1.315 5.504 2.105 2.780 3.355 4.180
    # 2011-04-18 4.785 5.206 0.653 5.395 5.491 1.280 5.486 2.053 2.727 3.311 4.142
    

    如果我们需要对“Tenor”列进行排序,可以选择将其转换为factor,并根据值指定levels

    library(gtools)
    df1$Tenor <- factor(df1$Tenor, levels = mixedsort(unique(df1$Tenor)))
    xtabs(`Bid Yield` ~ `Last Update` + Tenor, df1)
    #            Tenor
    #Last Update     1Y    2Y    3Y    4Y    5Y    7Y   10Y   15Y   20Y   25Y   30Y
    #  2011-04-15 0.666 1.315 2.105 2.780 3.355 4.180 4.807 5.233 5.411 5.504 5.504
    #  2011-04-18 0.653 1.280 2.053 2.727 3.311 4.142 4.785 5.206 5.395 5.491 5.486
    

    数据

    df1 <- structure(list(BidYield = c(0.666, 1.315, 2.105, 2.78, 3.355, 
    4.18, 4.807, 5.233, 5.411, 5.504, 5.504, 0.653, 1.28, 2.053, 
    2.727, 3.311, 4.142, 4.785, 5.206, 5.395, 5.491, 5.486), 
      LastUpdate = c("2011-04-15", 
    "2011-04-15", "2011-04-15", "2011-04-15", "2011-04-15", "2011-04-15", 
    "2011-04-15", "2011-04-15", "2011-04-15", "2011-04-15", "2011-04-15", 
    "2011-04-18", "2011-04-18", "2011-04-18", "2011-04-18", "2011-04-18", 
    "2011-04-18", "2011-04-18", "2011-04-18", "2011-04-18", "2011-04-18", 
    "2011-04-18"), Tenor = c("1Y", "2Y", "3Y", "4Y", "5Y", "7Y", 
    "10Y", "15Y", "20Y", "25Y", "30Y", "1Y", "2Y", "3Y", "4Y", "5Y", 
    "7Y", "10Y", "15Y", "20Y", "25Y", "30Y")), 
     class = "data.frame", row.names = c("1", 
    "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
    "14", "15", "16", "17", "18", "19", "20", "21", "22"))
    

    【讨论】:

    • 我试过了,但它不起作用,因为我在列名中有空格。我尝试使用 colnames
    • @Agi 在这种情况下,只需为间隔列名添加反引号
    • 它给我一个错误如下:terms.formula(formula, data = data) 中的错误:模型公式中的无效项
    • @Agi 您能否使用dput(yourdata) 并使用其输出更新您的帖子,以便我获得正确的数据结构以进行测试
    • 我明白了。我放的是'而不是`。对不起!谢谢
    猜你喜欢
    • 2019-04-10
    • 2019-01-27
    • 2015-11-14
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-26
    • 2013-11-18
    相关资源
    最近更新 更多