【发布时间】: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%")))
【问题讨论】:
-
欢迎堆栈溢出。请提供您的数据样本,以最大限度地提高您获得帮助的机会。我会推荐这个guideline。
If possible try pasting the results dput(head(yourtable)). -
我加了一个样本,保留了前四个硬币,因为有700多个,那会很长。希望我做对了