我想通过输入1进行映射并将列添加到输入2,以便获得如下所示的Desired Outcome。我可以看到如何在最后一位代码中实现我想要的单行Input 1,但是无法遍历Input 1的所有行。有没有办法用{{1}来实现它}?
purrr
试过这种方法:
library(tidyverse)
library(lubridate)
# Input 1
fc_periods <- data_frame(
period = c("m1", "m2", "m3"),
start = c(ymd("2018-01-01"), ymd("2018-02-01"), ymd("2018-03-01")),
end = c(ymd("2018-01-31"), ymd("2018-02-28"), ymd("2018-03-31")),
interval = interval(start, end)
)
# Input 2
unspread_data <- data_frame(
act_start = c(ymd("2017-11-01"), ymd("2018-01-10"), ymd("2018-02-14"), ymd("2017-12-01")),
act_end = c(ymd("2018-04-30"), ymd("2018-01-25"), ymd("2018-03-16"), ymd("2017-12-31")),
value = c(600, 100, 200, 999)
)
# Desired outcome
spread_data <- data_frame(
act_start = c(ymd("2017-11-01"), ymd("2018-01-10"), ymd("2018-02-14"), ymd("2017-12-01")),
act_end = c(ymd("2018-04-30"), ymd("2018-01-25"), ymd("2018-03-16"), ymd("2017-12-31")),
value = c(600, 100, 200, 999),
M1 = c(100, 100, 0, 0),
M2 = c(100, 0, 100, 0),
M3 = c(100, 0, 100, 0)
)
# If dealing with a single period, e.g. M1, then could do this:
start <- ymd("2018-01-01")
end <- ymd("2018-01-31")
interval <- interval(start, end)
spread_data <- unspread_data %>%
mutate(
overlap1 = if_else(act_start %within% interval & act_end %within% interval, as.numeric(act_end - act_start), 0),
overlap2 = if_else(act_start %within% interval, as.numeric(end - act_start), 0),
overlap3 = if_else(act_end %within% interval, as.numeric(act_end - start), 0),
overlap4 = if_else(act_start < start & act_end > end, as.numeric(end - start), 0),
days = as.numeric(act_end - act_start),
overlap = if_else(overlap1 > 0, overlap1,
if_else(overlap2 > 0, overlap2,
if_else(overlap3 > 0, overlap3,
if_else(overlap4 > 0, overlap4, 0)))),
fraction = if_else(days > 0, overlap / days, 0),
M1 = fraction * value
)
出现此错误:
spread_data <- fc_periods %>% pmap_df(function(period, start, end, interval){
unspread_data %>%
mutate(
overlap1 = if_else(act_start %within% interval & act_end %within% interval, as.numeric(act_end - act_start), 0),
overlap2 = if_else(act_start %within% interval, as.numeric(end - act_start), 0),
overlap3 = if_else(act_end %within% interval, as.numeric(act_end - start), 0),
overlap4 = if_else(act_start < start & act_end > end, as.numeric(end - start), 0),
days = as.numeric(act_end - act_start),
overlap = if_else(overlap1 > 0, overlap1,
if_else(overlap2 > 0, overlap2,
if_else(overlap3 > 0, overlap3,
if_else(overlap4 > 0, overlap4, 0)))),
fraction = if_else(days > 0, overlap / days, 0),
period = fraction * value
)
})
答案 0 :(得分:0)
这很有效。似乎问题是尝试在 int height;
int width;
double left;
double top;
private void MainWindow_SizeChanged
{
height = this.Height;
width = this.Widthl
left = this.Left;
top = this.Top;
}
private void ShowMainWindow(object sender, EventArgs e)
{
this.Height = height;
this.Width = width;
this.Left = left;
this.Top = top;
}
内的函数中使用%within%
或interval
(不确定哪个是问题)。仅使用开始日期和结束日期就可以解决问题。
pmap