Я пытаюсь использовать данные бейсбольной игры в ретроспективе, чтобы она называлась ловцом для любого заданного шага, извлеченного в эпоху pitfx.Я использовал ifelse для запуска серии тестов, чтобы найти начальный ловец.Код и ошибки приведены ниже - также я намерен после того, как заставить это работать, вместо того, чтобы печатать «нет», в случае неудачи, будет иметь подобные ifelse-тесты, вложенные в это, пока я не проработаю от HmBat1Pos до HmBat9Pos ... и т.д.
atbat$starting_catcher_retro = ifelse(((atbat$date = retro_games$Date)
& (atbat$pitcher_name = retro_games$HmStPchNm)
& (atbat$num = 1)
& (atbat$inning_side = "top")
& (retro_games$HmBat1Pos = 2)),
retro_games$HmBat1ID, "no")
ошибки
Error in `$<-.data.frame`(`*tmp*`, date, value = c(13963, 13964, 13968, :
replacement has 26726 rows, data has 2146373
Затем я попытался извлечь часть кода, проверяющего дату, и все равно получил ошибки.Поэтому я запустил это вместо
atbat$starting_catcher_retro = ifelse(((atbat$num = 1)
& (atbat$inning_side = "top")
&(retro_games$HmBat1Pos = 2)),
retro_games$HmBat1ID, "no")
и получил эти другие ошибки
Error in (atbat$num = 1) & (atbat$inning_side = "top") :
operations are possible only for numeric, logical or complex types
ответ на комментарии
это был код, который я добавил, и ошибкавернул
> merged_df <- merge(atbat, retro_games,
+ by.x = c("date", "pitcher_name"),
+ by.y = c("Date", "HmStPchNm"), all.x = FALSE)
>
> merged_df$starting_catcher_retro = with(merged_df,
+ ifelse((num == 1)
+ & (inning_side == "top")
+ & (HmBat3Pos == 2),
+ HmBat3ID, "no"))
>
> atbat$starting_catcher_retro <- merged_df$starting_catcher_retro[match(merged_df$unique_id, atbat$unique_id)]
Error in `$<-.data.frame`(`*tmp*`, starting_catcher_retro, value = c("no", :
replacement has 566448 rows, data has 2146373
Я также попытался применить другой подход к описанному выше, основанный на идее, которая была у меня в голове - ниже приведен этот код и возвращены ошибки, где все выглядит так, как будто оно должно работать, но тамдаже новый столбец не создан
atbat$starting_catcher_retro = ifelse(((retro_games$Date %in% atbat$date)
& (retro_games$HmStPchNm %in% atbat$pitcher_name)
& (atbat$inning_side == "top")
& (retro_games$HmBat1Pos == 2)),
retro_games$HmBat1ID,
ifelse(((retro_games$Date %in% atbat$date)
& (retro_games$HmStPchNm %in% atbat$pitcher_name)
& (atbat$inning_side == "top")
& (retro_games$HmBat2Pos == 2)),
retro_games$HmBat2ID,
ifelse(((retro_games$Date %in% atbat$date)
& (retro_games$HmStPchNm %in% atbat$pitcher_name)
& (atbat$inning_side == "top")
& (retro_games$HmBat3Pos == 2)),
retro_games$HmBat3ID,
ifelse(((retro_games$Date %in% atbat$date)
& (retro_games$HmStPchNm %in% atbat$pitcher_name)
& (atbat$inning_side == "top")
& (retro_games$HmBat4Pos == 2)),
retro_games$HmBat4ID,
ifelse(((retro_games$Date %in% atbat$date)
& (retro_games$HmStPchNm %in% atbat$pitcher_name)
& (atbat$inning_side == "top")
& (retro_games$HmBat5Pos == 2)),
retro_games$HmBat5ID,
ifelse(((retro_games$Date %in% atbat$date)
& (retro_games$HmStPchNm %in% atbat$pitcher_name)
& (atbat$inning_side == "top")
& (retro_games$HmBat6Pos == 2)),
retro_games$HmBat6ID,
ifelse(((retro_games$Date %in% atbat$date)
& (retro_games$HmStPchNm %in% atbat$pitcher_name)
& (atbat$inning_side == "top")
& (retro_games$HmBat7Pos == 2)),
retro_games$HmBat7ID,
ifelse(((retro_games$Date %in% atbat$date)
& (retro_games$HmStPchNm %in% atbat$pitcher_name)
& (atbat$inning_side == "top")
& (retro_games$HmBat8Pos == 2)),
retro_games$HmBat8ID,
ifelse(((retro_games$Date %in% atbat$date)
& (retro_games$HmStPchNm %in% atbat$pitcher_name)
& (atbat$inning_side == "top")
& (retro_games$HmBat9Pos == 2)),
retro_games$HmBat9ID,
ifelse(((retro_games$Date %in% atbat$date)
& (retro_games$VisStPchNm %in% atbat$pitcher_name)
& (atbat$inning_side == "bottom")
& (retro_games$VisBat1Pos == 2)),
retro_games$VisBat1ID,
ifelse(((retro_games$Date %in% atbat$date)
& (retro_games$VisStPchNm %in% atbat$pitcher_name)
& (atbat$inning_side == "bottom")
& (retro_games$VisBat2Pos == 2)),
retro_games$VisBat12D,
ifelse(((retro_games$Date %in% atbat$date)
& (retro_games$VisStPchNm %in% atbat$pitcher_name)
& (atbat$inning_side == "bottom")
& (retro_games$VisBat3Pos == 2)),
retro_games$VisBat3ID,
ifelse(((retro_games$Date %in% atbat$date)
& (retro_games$VisStPchNm %in% atbat$pitcher_name)
& (atbat$inning_side == "bottom")
& (retro_games$VisBat4Pos == 2)),
retro_games$VisBat4ID,
ifelse(((retro_games$Date %in% atbat$date)
& (retro_games$VisStPchNm %in% atbat$pitcher_name)
& (atbat$inning_side == "bottom")
& (retro_games$VisBat5Pos == 2)),
retro_games$VisBat5ID,
ifelse(((retro_games$Date %in% atbat$date)
& (retro_games$VisStPchNm %in% atbat$pitcher_name)
& (atbat$inning_side == "bottom")
& (retro_games$VisBat6Pos == 2)),
retro_games$VisBat6ID,
ifelse(((retro_games$Date %in% atbat$date)
& (retro_games$VisStPchNm %in% atbat$pitcher_name)
& (atbat$inning_side == "bottom")
& (retro_games$VisBat7Pos == 2)),
retro_games$VisBat7ID,
ifelse(((retro_games$Date %in% atbat$date)
& (retro_games$VisStPchNm %in% atbat$pitcher_name)
& (atbat$inning_side == "bottom")
& (retro_games$VisBat8Pos == 2)),
retro_games$VisBat8ID,
ifelse(((retro_games$Date %in% atbat$date)
& (retro_games$VisStPchNm %in% atbat$pitcher_name)
& (atbat$inning_side == "bottom")
& (retro_games$VisBat9Pos == 2)),
retro_games$VisBat9ID, ""))))))))))))))))))
, а ошибка
Error in ans[test & ok] <- rep(yes, length.out = length(ans))[test & ok] :
replacement has length zero
In addition: There were 23 warnings (use warnings() to see them)
> warnings()
Warning messages:
1: In (retro_games$Date %in% atbat$date) & (retro_games$HmStPchNm %in% ... :
longer object length is not a multiple of shorter object length
2: In (retro_games$Date %in% atbat$date) & (retro_games$HmStPchNm %in% ... :
longer object length is not a multiple of shorter object length
3: In (retro_games$Date %in% atbat$date) & (retro_games$HmStPchNm %in% ... :
longer object length is not a multiple of shorter object length
4: In (retro_games$Date %in% atbat$date) & (retro_games$HmStPchNm %in% ... :
longer object length is not a multiple of shorter object length
5: In (retro_games$Date %in% atbat$date) & (retro_games$HmStPchNm %in% ... :
longer object length is not a multiple of shorter object length
6: In (retro_games$Date %in% atbat$date) & (retro_games$HmStPchNm %in% ... :
longer object length is not a multiple of shorter object length
7: In (retro_games$Date %in% atbat$date) & (retro_games$HmStPchNm %in% ... :
longer object length is not a multiple of shorter object length
8: In (retro_games$Date %in% atbat$date) & (retro_games$HmStPchNm %in% ... :
longer object length is not a multiple of shorter object length
9: In (retro_games$Date %in% atbat$date) & (retro_games$HmStPchNm %in% ... :
longer object length is not a multiple of shorter object length
10: In (retro_games$Date %in% atbat$date) & (retro_games$HmStPchNm %in% ... :
longer object length is not a multiple of shorter object length
11: In (retro_games$Date %in% atbat$date) & (retro_games$HmStPchNm %in% ... :
longer object length is not a multiple of shorter object length
12: In (retro_games$Date %in% atbat$date) & (retro_games$HmStPchNm %in% ... :
longer object length is not a multiple of shorter object length
13: In (retro_games$Date %in% atbat$date) & (retro_games$HmStPchNm %in% ... :
longer object length is not a multiple of shorter object length
14: In (retro_games$Date %in% atbat$date) & (retro_games$HmStPchNm %in% ... :
longer object length is not a multiple of shorter object length
15: In (retro_games$Date %in% atbat$date) & (retro_games$HmStPchNm %in% ... :
longer object length is not a multiple of shorter object length
16: In (retro_games$Date %in% atbat$date) & (retro_games$HmStPchNm %in% ... :
longer object length is not a multiple of shorter object length
17: In (retro_games$Date %in% atbat$date) & (retro_games$HmStPchNm %in% ... :
longer object length is not a multiple of shorter object length
18: In (retro_games$Date %in% atbat$date) & (retro_games$HmStPchNm %in% ... :
longer object length is not a multiple of shorter object length
19: In (retro_games$Date %in% atbat$date) & (retro_games$VisStPchNm %in% ... :
longer object length is not a multiple of shorter object length
20: In (retro_games$Date %in% atbat$date) & (retro_games$VisStPchNm %in% ... :
longer object length is not a multiple of shorter object length
21: In (retro_games$Date %in% atbat$date) & (retro_games$VisStPchNm %in% ... :
longer object length is not a multiple of shorter object length
22: In (retro_games$Date %in% atbat$date) & (retro_games$VisStPchNm %in% ... :
longer object length is not a multiple of shorter object length
23: In rep(yes, length.out = length(ans)) :
'x' is NULL so the result will be NULL
edit2
выборка из обоих кадров данных первых 15 строк была слишком длиннойвключить сюда пастбище этого - https://pastebin.com/kTVEgdRs