Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将数据帧的子集与子集两侧最近的观测值按因子包装_R_Ggplot2_Dplyr - Fatal编程技术网

R 将数据帧的子集与子集两侧最近的观测值按因子包装

R 将数据帧的子集与子集两侧最近的观测值按因子包装,r,ggplot2,dplyr,R,Ggplot2,Dplyr,我有一个非常大的数据帧(大约1GB和100万行),从上午10点到下午4点都有观测结果。 我想用geom_step绘制一些绘图,精确地放大1:00:00-1:00:05pm,因此我认为为了节省RAM和时间,将原始数据子集会更快。(而不是绘制所有内容并随后放大)。不幸的是,绘制所有内容不是一个选项。 因为我使用的是geom_步骤,所以我需要用before和after值来包装我的子集,否则就无法正确绘制 下面是一个示例数据集,它实际上比我的数据小得多,但其原理如下: set.seed(2) c1 &l

我有一个非常大的数据帧(大约1GB和100万行),从上午10点到下午4点都有观测结果。 我想用geom_step绘制一些绘图,精确地放大1:00:00-1:00:05pm,因此我认为为了节省RAM和时间,将原始数据子集会更快。(而不是绘制所有内容并随后放大)。不幸的是,绘制所有内容不是一个选项。 因为我使用的是geom_步骤,所以我需要用before和after值来包装我的子集,否则就无法正确绘制

下面是一个示例数据集,它实际上比我的数据小得多,但其原理如下:

set.seed(2)
c1 <- sort( sample(round(runif(10,1,10)*10), 10 , replace = TRUE ) )
c2 <- sample(c("A", "C", "T"), 10, replace = TRUE)
c4 <- round(runif(10)*1000)
d1 <- c(5, 12, c1, 96, 97, 98, 100)
d2 <- c("B", "C", c2, "B", "T", "T", "A")
d3 <- c(300, 400, c4, 200, 300, 300, 100)
dcat2 <- c(sample(1:2, 14, replace = TRUE), 1,1)

mydat <- data.frame(time = d1, category = d2, category2 = dcat2 , inventory = d3)
print(mydat)
#      time category category2 inventory
#  1     5        B         2       300
#  2    12        C         1       400
#  3    27        C         1        10
#  4    52        C         1       165
#  5    59        T         2       810
#  6    62        A         2       869
#  7    62        C         2       514
#  8    73        C         1       627
#  9    85        A         2       844
#  10   95        C         2       285
#  11   95        T         1       667
#  12   95        A         1       150
#  13   96        B         2       200
#  14   97        T         2       300
#  15   98        T         1       300
#  16  100        A         1       100

require(ggplot2)
p <- ggplot(data=mydat, aes(x = time, y = inventory, group = category, col = category)) + 
     geom_step() +
     facet_grid(.~category2)
print(p)
q <- ggplot(data=mydat, aes(x = time, y = inventory, group = category, col = category)) + 
     geom_step() +
     facet_grid(.~category2) +
     coord_cartesian(xlim = c(80,98))
print(q)
打印(r)
出现错误, 因为
geom_step
在平面中绘制图形时至少需要2个点,但其中一些点已被时间80到98之间的子集截断


请为每个因素提供一种有效的方法来查找缩放窗口之前的最后一个观察结果,以及复制最后一个观察结果并将缩放窗口包装在这些(或其他解决方案)中的方法。

好的,这是一种黑客行为,但它可能会帮助您暂时解决这个问题(即,直到有人提供更干净的解决方案为止)

这将创建一个临时变量,其中包含每个因子的发生次数。然后对结果进行过滤,以确保至少有2个数据点

mydat.zoom <-
    filter(mydat, time >80, time < 98) %>%  # Your current filtering
    group_by(category2)                %>%  # Using cat2 for this example
    mutate(cat2_cnt = n())             %>%  # count to be filtered on
    filter(cat2_cnt > 1)               %>%  # Ensure >= 2 data points
    ungroup                            %>%  # Don't need grouping.
    select(-fcnt)                           # Don't need column anymore

以下是我用来将过去的观察结果带入正在检查的时间窗口的方法:

require(data.table)
myDT <- as.data.table(mydat)
preDT <- myDT[ time < 80, .SD[.N] , by = .(category, category2)] # for each category/category2 interaction before time =80, take the last observation
mydat.zoom <- rbind(preDT, myDT[ time >= 80 & time <= 98] ) # gives everything from time = 80 to time =98 and the last observation for each thing that happened before time = 80 
require(data.table)

myDT“c3”似乎缺失。从那时起,我最终解决了这个问题,但没有时间重新审视我的问题以添加解决方案。我使用了data.table包(也可以使用dplyr),但是下面的解决方案可以有效地解决这个问题。require(data.table)mydat.zoom=80&time hi,98],mydat[time>98,.SD[1],by=(category1,category2)])谢谢您的回复!不过,我还想把那个时期只发生过一次的观察结果也包括在内。我确实解决了我发布后遇到的问题,并且我将它包含在了另一条评论中,供搜索同一问题的人使用
mydat.zoom <-
    filter(mydat, time >80, time < 98) %>%  # Your current filtering
    group_by(category2)                %>%  # Using cat2 for this example
    filter(n() > 1)                    %>%  # Ensure >= 2 data points
    ungroup                                 # Don't need grouping.
require(data.table)
myDT <- as.data.table(mydat)
preDT <- myDT[ time < 80, .SD[.N] , by = .(category, category2)] # for each category/category2 interaction before time =80, take the last observation
mydat.zoom <- rbind(preDT, myDT[ time >= 80 & time <= 98] ) # gives everything from time = 80 to time =98 and the last observation for each thing that happened before time = 80