我想通过从模拟表(table\simul)生成航班号来模拟航班号,以观察表为基础.
table_simul <- data.table(
date_f = c("2020-01-01","2020-01-02","2020-01-03","2020-01-03"),
city = c("Paris","Paris","London", "Berlin")
)
table_obs <- data.table(
city = c("Paris","Paris","Paris","London","London", "Berlin"),
flight = c(1,2,7,4,5,14),
weight = c(0.33,0.33,0.33,0.5,0.5,1)
)
使用的数据:
Table simul:
date city
2020-01-01 Paris
2020-01-01 Paris
2020-01-01 London
2020-01-01 Berlin
Table obs
---------
city flight weight
Paris 1 0.33
Paris 2 0.33
Paris 7 0.33
London 4 0.5
London 5 0.5
Berlin 14 1
预期结果:
date_f city flight
2020-01-01 Paris 2
2020-01-02 Paris 2
2020-01-03 London 4
2020-01-03 Berlin 14
我想使用数据表包,因为数据量非常大.
get_flight_sample <- function(param_city){
table_simul[city==param_city]
res <- sample(table_obs$flight,1, replace=T, prob = table_obs$weight)
}
res <- table_simul[,.(flight = get_flight_sample(city))]