【问题标题】:Check which interval values lies within for each day, and create new matrix containing information of which interval in R检查每天的间隔值,并创建包含 R 中哪个间隔的信息的新矩阵
【发布时间】:2020-04-08 09:23:16
【问题描述】:

我正在使用一个名为 price.daily

的数据集,其中包含 2014 年 1 月 1 日至 2019 年 12 月 31 日期间 700 多种加密货币的价格信息
            Bitcoin     Ethereum    XRP      Bitcoin.SV    Stellar    ...
   ...
2018-01-01  13657.20    772.64      2.39     NA            0.480008
2018-01-02  14982.10    884.44      2.48     NA            0.564766
2018-01-03  15201.00    962.72      3.11     NA            0.896227
   ... 

然后我每天使用 sapply 计算分位数,就像另一个问题所建议的那样,这很好用

col.daily <- seq(1,length(price.daily$Bitcoin))
quantile.daily = sapply(col.daily, function(y) {quantile(x = unlist(price.daily[y,] ), seq(0,1, length=6),na.rm = TRUE )})
quantile.daily.t = t(quantile.daily)
rownames(quantile.daily.t) = rownames(price.daily)

从中我得到我的间隔应该是的数字

             0%         20%         40%         60%         80%     100%
   ...
2018-01-01   2.60e-05   0.1681120   0.7189722   2.3060000   9.392   13657.20
2018-01-02   3.40e-05   0.1946376   0.7232178   2.4240000   10.092  14982.10
2018-01-03   3.80e-05   0.1982452   0.7771724   2.4820000   10.054  15201.00
   ...

然后我想做的是每天获取每种加密货币的价格,并检查它位于哪个区间内,如果没有可用数据,则创建一个包含数字 1 到 5 和 NA 的新矩阵。应该出来

            Bitcoin   Ethereum    XRP     Bitcoin.SV    Stellar   ...
   ...
2018-01-01  5         5           4       NA            2
2018-01-02  5         5           4       NA            2
2018-01-03  5         5           4       NA            3
   ...

我想我也可以为此使用 sapply

使用 dput(head(price.daily)) 作为我的 price.daily 数据的数据样本

structure(list(Bitcoin = c(771.4, 802.39, 818.72, 859.51, 933.53, 
953.29), Ethereum = c(NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_), XRP = c(0.026944, 0.028726, 0.027627, 0.028129, 
0.02523, 0.0257), Bitcoin.Cash = c(NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_), row.names = c("2014-01-01", 
"2014-01-02", "2014-01-03", "2014-01-04", "2014-01-05", "2014-01-06"
), class = "data.frame")

对于分位数

structure(c(0.00044, 0.000353, 0.000303, 0.000301, 0.000271, 
0.00001, 0.0330034, 0.0319948, 0.0327684, 0.0318646, 0.0274614, 
0.0237276, 0.161692, 0.1793948, 0.163744, 0.1610448, 0.1579238, 
0.0728448, 3.014, 3.728, 3.85, 3.87, 3.814, 2.54200000000001, 
6.036, 7.578, 7.14, 7.434, 7.474, 7.188, 771.4, 802.39, 818.72, 
859.51, 933.53, 953.29), .Dim = c(6L, 6L), .Dimnames = list(c("2014-01-01", 
"2014-01-02", "2014-01-03", "2014-01-04", "2014-01-05", "2014-01-06"
), c("0%", "20%", "40%", "60%", "80%", "100%")))

【问题讨论】:

  • 欢迎堆栈溢出。请提供您的数据样本,以最大限度地提高您获得帮助的机会。我会推荐这个guidelineIf possible try pasting the results dput(head(yourtable)).
  • 我加了一个样本,保留了前四个硬币,因为有700多个,那会很长。希望我做对了

标签: r intervals sapply


【解决方案1】:

函数findInterval 完全满足您的需求。唯一的困难在于将其应用于正确的数据。

带循环的简单解决方案:

result_loop = price.daily
for (i in seq_len(nrow(price.daily))) {
  result_loop[i, ] = findInterval(price.daily[i, ], quantile.daily[i, ])
}

没有循环的替代解决方案:

combined = cbind(price.daily, quantile.daily)
result_alternative = as.data.frame(t(apply(combined, 1, function(x) findInterval(x[1:ncol(price.daily)], x[(1 + ncol(price.daily)):ncol(combined)]))))
colnames(result_alternative) = colnames(price.daily)

第二种解决方案(受this answer to a similar question 启发)有一些额外的问题,例如combined 变量的内存开销。即使不是这种情况,我仍然会使用第一个解决方案。使用语言结构来避免循环可能很诱人,但在许多情况下它会使调试和维护变得更加困难。

附带说明:结果可以是矩阵而不是数据框,但由于price.daily (不必要地)作为数据框给出,我选择对结果使用相同的类。

【讨论】:

    猜你喜欢
    • 2020-09-22
    • 1970-01-01
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    相关资源
    最近更新 更多