我正在尝试使用 data.table 在 R 中合并一堆重叠的时间段。我接到了一个电话,要求自己对桌子进行 foverlap,这已经足够高效了。
我的问题是这样的:说 A 期与 B 期重叠,B 期与 C 期重叠,但 A 与 C 不重叠。在这种情况下,A 不与 C 分组,它们最终必须合并。
目前我有一个 while 循环查找重叠和合并,直到不再发生合并,但这并不是完全可扩展的。我可以看到的一个解决方案是将组的索引递归地应用于自身直到稳定,但这看起来仍然需要一个循环,我想要一个完全矢量化的解决方案。
dt = data.table(start = c(1,2,4,6,8,10),
end = c(2,3,6,8,10,12))
setkeyv(dt,c("start","end"))
f = foverlaps(dt,
dt,
type="any",
mult="first",
which="TRUE")
#Needs to return [1,1,3,3,3,3]
print(f)
#1 1 3 3 4 5
print(f[f])
#1 1 3 3 3 4
print(f[f][f])
#1 1 3 3 3 3
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助我提出一些有关矢量化此程序的想法吗?
使用 ID 进行编辑:
dt = data.table(id = c('A','A','A','A','A','B','B','B'),
eventStart = c(1,2,4,6,8,10,11,15),
eventEnd = c(2,3,6,8,10,12,14,16)) …Run Code Online (Sandbox Code Playgroud) 我有一个 R 闪亮页面,正在根据单击饼图过滤数据。如果我可以通过单击图例条目触发相同的过滤事件,那就太好了,但我似乎找不到事件触发器,因此它只是过滤该图表而不传播到其他图表。图例点击事件是否可以访问?
library(data.table)
library(plotly)
library(shiny)
dt = as.data.table(mtcars)
ui <- fluidPage(
plotlyOutput("pie1"),
plotlyOutput("pie2")
)
server <- function(input, output){
gearDT = reactive({
return(dt[,.N,by=gear])
})
cylDT = reactive({
return(dt[,.N,by=cyl])
})
output$pie1 <- renderPlotly({
plot_ly(gearDT(), labels = ~gear, values = ~N, type = "pie") %>%
layout(showlegend = TRUE)
})
output$pie2 <- renderPlotly({
plot_ly(cylDT(), labels = ~cyl, values = ~N, type = "pie") %>%
layout(showlegend = TRUE)
})
}
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud) 当尝试在不同方面创建差异 geom_rect 时,我遇到了一些奇怪的行为。在下面的示例中,geom_point 的颜色应与 geom_rect 的填充颜色相同,填充颜色应与 geom_rect 的轮廓相同。然而,这些因素似乎在某个地方以一种奇怪的方式串联起来。
任何帮助,将不胜感激。
我不希望点按序列着色,因为簇和序列并不总是相同(它用于可视化排列统计量)。
这些显然不是我想要绘制的实际数据!
library(data.table)
library(ggplot2)
scatter.dt = data.table(sequence = factor(paste('Sequence',c(1,2,3,4))),
cluster = factor(paste('Cluster',c(1,2,3,4))),
outcome = c(1,1,1,1),
transition.time = 1:4,
intervention.time = 1:4)
vline.dt = data.table(sequence = scatter.dt$sequence,
cluster = scatter.dt$cluster,
transition.time = 1:4,
intervention.time = 2:5)
plot1 = ggplot2::ggplot() +
ggplot2::geom_rect(data = vline.dt,
aes(fill = sequence,
colour = sequence,
xmin = transition.time,
xmax = intervention.time,
ymin = -Inf,
ymax = Inf),
alpha = .6,
size = 2) +
ggplot2::geom_point(data=scatter.dt,
aes(x=transition.time ,
y=intervention.time, …Run Code Online (Sandbox Code Playgroud) 正在寻找一种简单的方法来使用 Python 3 中的 win32com 获取 Excel 工作簿中的工作表列表。我希望避免迭代,但找不到执行此操作的函数。