gpt4 book ai didi

r - data.table 无法识别过滤器中的逻辑

转载 作者:行者123 更新时间:2023-12-04 21:11:02 24 4
gpt4 key购买 nike

在以下代码段中,data.table 在 i 中使用时似乎无法识别逻辑。

我在最小示例中重现问题的所有尝试都失败了,这就是我在此处发布完整部分的原因。我希望它与“as.logical(cumsum(CURRENT_TRIP))”部分有关,但只是一种直觉......

# Testdata
timetable <- data.table(rbind(
c("r1", "t1_1", "p1", 10, 10),
c("r1", "t1_1", "p2", 11, 11),
c("r1", "t1_1", "p3", 12, 12),
c("r1", "t1_1", "p4", 13, 13),
c("r1", "t1_1", "p5", 14, 14),
c("r1", "t1_1", "p6", 15, 15),
c("r1", "t1_1", "p7", 16, 16),
c("r1", "t1_1", "p8", 17, 17),
c("r1", "t1_1", "p9", 18, 18),
c("r1", "t1_1", "p10", 19, 19),

c("r2", "t2", "p11", 9, 9),
c("r2", "t2", "p12", 10, 10),
c("r2", "t2", "p3", 11, 11),
c("r2", "t2", "p13", 12, 12),
c("r2", "t2", "p14", 13, 13),
c("r2", "t2", "p15", 14, 14),
c("r2", "t2", "p16", 15, 15),
c("r2", "t2", "p17", 16, 16),
c("r2", "t2", "p18", 17, 17)
))
setnames(timetable, c("ROUTE", "TRIP", "STOP", "ARRIVAL", "DEPARTURE"))
timetable[, ':='(ARRIVAL = as.integer(ARRIVAL), DEPARTURE = as.integer(DEPARTURE))]


# Input
startStation <- "p3"
startTime <- 8

setorder(timetable, TRIP, ARRIVAL)
timetable[, ID := .I]

timetable[,':='(ARR_ROUND_PREV = Inf, ARR_ROUND = Inf, ARR_BEST = Inf, MARKED = F, CURRENT_TRIP = F)]
timetable[STOP == startStation, ':='(ARR_ROUND_PREV = startTime, ARR_ROUND = startTime, ARR_BEST = startTime, MARKED = T)]

routes <- timetable[MARKED == T, unique(ROUTE)]
ids <- timetable[MARKED == T & DEPARTURE > ARR_ROUND, .(ID = ID[DEPARTURE == min(DEPARTURE)]), by = ROUTE][, ID]

timetable[ID %in% ids, CURRENT_TRIP := T]
timetable[, MARKED := F]

trips <- timetable[CURRENT_TRIP == T, unique(TRIP)]
timetable[TRIP %in% trips, CURRENT_TRIP := as.logical(cumsum(CURRENT_TRIP)), by = TRIP]

# ?
timetable
nrow(timetable[CURRENT_TRIP == T]) #8
sum(timetable$CURRENT_TRIP == T) #15

# but
nrow(timetable[CURRENT_TRIP > 0]) #15
nrow(timetable[CURRENT_TRIP == 1L]) #15

有任何想法吗?

在 Win 64 位上使用最新的 1.9.7 和 1.9.6 以及 R 3.2.3 出现问题

晶圆厂

最佳答案

你有和我完全一样的错误!!!

Strange issue with data.table row search

我也无法用最少的代码重现它!

我对您的代码的解决方案是更改您设置列 CURRENT_TRIP 的方式。

timetable[ID %in% ids]$CURRENT_TRIP <- T
timetable[, MARKED := F]

trips <- timetable[CURRENT_TRIP == T, unique(TRIP)]
timetable[TRIP %in% trips]$CURRENT_TRIP <- timetable[,as.logical(cumsum(CURRENT_TRIP)), by = TRIP]$V1

# ?
timetable
nrow(timetable[CURRENT_TRIP == T]) #8
sum(timetable$CURRENT_TRIP == T) #15

# but
nrow(timetable[CURRENT_TRIP > 0]) #15
nrow(timetable[CURRENT_TRIP == 1L]) #15

使用 dT[,Column:=T] 符号设置列也导致了我同样的问题!我不知道为什么,我正在与 data.tables 的创建者联系以解决这个问题!

关于r - data.table 无法识别过滤器中的逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34433063/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com