1

给定以下数据格式,如果 D1 是 R 中的矩阵,我将如何每天在 0630 和 1030(含)时间之间用单个随机 1 填充列 X2?每天只有一个 1,那个 1 随机出现。实际数据要长得多,可能是 1000 天或更长时间,所以我最终会得到 1000 个 1 都放在 0630 和 1030 之间。

请注意,虽然这是 5 分钟的数据,但我想找到一个也适用于其他时间段的解决方案。(15 分钟、1 分钟等)如有必要,这可能是第二步。

D1 <- structure(c(1110920, 1110920, 1110920, 1110920, 1110920, 1110920, 
1110920, 1110920, 1110920, 1110920, 1110920, 1110920, 1110920, 
1110920, 1110920, 1110920, 1110920, 1110920, 1110920, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 1110921, 
1110921, 1110921, 1110921, 1110921, 1110921, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 1110922, 
1110922, 1110922, 1110922, 1110922, 2225, 2230, 2235, 2240, 2245, 
2250, 2255, 2300, 2305, 2310, 2315, 2320, 2325, 2330, 2335, 2340, 
2345, 2350, 2355, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 
100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 200, 
205, 210, 215, 220, 225, 230, 235, 240, 245, 250, 255, 300, 305, 
310, 315, 320, 325, 330, 335, 340, 345, 350, 355, 400, 405, 410, 
415, 420, 425, 430, 435, 440, 445, 450, 455, 500, 505, 510, 515, 
520, 525, 530, 535, 540, 545, 550, 555, 600, 605, 610, 615, 620, 
625, 630, 635, 640, 645, 650, 655, 700, 705, 710, 715, 720, 725, 
730, 735, 740, 745, 750, 755, 800, 805, 810, 815, 820, 825, 830, 
835, 840, 845, 850, 855, 900, 905, 910, 915, 920, 925, 930, 935, 
940, 945, 950, 955, 1000, 1005, 1010, 1015, 1020, 1025, 1030, 
1035, 1040, 1045, 1050, 1055, 1100, 1105, 1110, 1115, 1120, 1125, 
1130, 1135, 1140, 1145, 1150, 1155, 1200, 1205, 1210, 1215, 1220, 
1225, 1230, 1235, 1240, 1245, 1250, 1255, 1300, 1305, 1310, 1315, 
1335, 1340, 1345, 1350, 1355, 1400, 1405, 1410, 1415, 1420, 1425, 
1430, 1505, 1510, 1515, 1520, 1525, 1530, 1535, 1540, 1545, 1550, 
1555, 1600, 1605, 1610, 1615, 1620, 1625, 1630, 1635, 1640, 1645, 
1650, 1655, 1700, 1705, 1710, 1715, 1720, 1725, 1730, 1735, 1740, 
1745, 1750, 1755, 1800, 1805, 1810, 1815, 1820, 1825, 1830, 1835, 
1840, 1845, 1850, 1855, 1900, 1905, 1910, 1915, 1920, 1925, 1930, 
1935, 1940, 1945, 1950, 1955, 2000, 2005, 2010, 2015, 2020, 2025, 
2030, 2035, 2040, 2045, 2050, 2055, 2100, 2105, 2110, 2115, 2120, 
2125, 2130, 2135, 2140, 2145, 2150, 2155, 2200, 2205, 2210, 2215, 
2220, 2225, 2230, 2235, 2240, 2245, 2250, 2255, 2300, 2305, 2310, 
2315, 2320, 2325, 2330, 2335, 2340, 2345, 2350, 2355, 0, 5, 10, 
15, 20, 25, 30, 35, 40, 45, 50, 55, 100, 105, 110, 115, 120, 
125, 130, 135, 140, 145, 150, 155, 200, 205, 210, 215, 220, 225, 
230, 235, 240, 245, 250, 255, 300, 305, 310, 315, 320, 325, 330, 
335, 340, 345, 350, 355, 400, 405, 410, 415, 420, 425, 430, 435, 
440, 445, 450, 455, 500, 505, 510, 515, 520, 525, 530, 535, 540, 
545, 550, 555, 600, 605, 610, 615, 620, 625, 630, 635, 640, 645, 
650, 655, 700, 705, 710, 715, 720, 725, 730, 735, 740, 745, 750, 
755, 800, 805, 810, 815, 820, 825, 830, 835, 840, 845, 850, 855, 
900, 905, 910, 915, 920, 925, 930, 935, 940, 945, 950, 955, 1000, 
1005, 1010, 1015, 1020, 1025, 1030, 1035, 1040, 1045, 1050, 1055, 
1100, 1105, 1110, 1115, 1120, 1125, 1130, 1135, 1140, 1145, 1150, 
1155, 1200, 1205, 1210, 1215, 1220, 1225, 1230, 1235, 1240, 1245, 
1250, 1255, 1300, 1305, 1310, 1315, 1335, 1340, 1345, 1350, 1355, 
1400, 1405, 1410, 1415, 1420, 1425, 1430, 1435, 1505, 1510, 1515, 
1520, 1525, 1530, 1535, 1540, 1545, 1550, 1555, 1600, 1605, 1610, 
1615, 1620, 1625, 1630, 1635, 1640, 1645, 1650, 1655, 1700, 1705, 
1710, 1715, 1720, 1725, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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date", 
"Time", "X2")))
4

2 回答 2

2
library("plyr")
resample <- function(x, ...) x[sample.int(length(x), ...)] # from `sample` documentation
D2 <- as.matrix(ddply(as.data.frame(D1), .(Date), function(DF) {
  intime <- which(DF$Time >= 630 & DF$Time <= 1030)
  if (length(intime) > 0) {
    DF[resample(intime,1),"X2"] <- 1
  }
  DF
}))

这会将矩阵转换为 data.frame,使用ddplyfromplyr来查看一天中的一天,然后匿名函数找到属于您给定时间范围内的条目的索引,随机选择一个(使用samplevia resample)并为其分配 1。整个事情又变成了一个矩阵(因为它就是这样开始的)。

与其显示整个输出,不如D2看看与D1

> which(D2 != D1, arr.ind = TRUE)
     row col
[1,] 117   3
[2,] 405   3
于 2012-10-09T18:57:05.613 回答
0

我更改了 D1 对象。

D1 [ , "X2"] <-  ave(D1[, "Time"] , D1[ , "Date"], 
         FUN= function(x){ x[sample( length(x), 1)] <- 1; return (x) } )
 unique(D1[ , "Date"])
#[1] 1110920 1110921 1110922
 which(D1 == 1, arr.ind = TRUE)
# ----
     row col
[1,]  11   3
[2,]  51   3
[3,] 419   3

这通过将每日“时间”发送到函数sample,然后从这些“时间”的长度返回单个伪随机行值来工作。在ave函数中,该值用作设置单个随机 1 的索引。ave返回与 d["X2"] 长度相同的向量

于 2012-10-09T18:58:54.897 回答