我试图使用后张棒球比赛的数据名称,捕手,为任何给定的球场拉在投手时代。我一直在使用catcher来运行一系列的测试来找到起始捕手。代码和错误都在下面--我还打算在让它正常工作之后,而不是在失败时打印"no“,在这里嵌套类似的如果其他测试,直到我完成从HmBat1Pos到HmBat9Pos...etc的工作。
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 NULLedit2
从前15行的两个数据中提取的示例太长,无法包含其中的一个pastebin https://pastebin.com/kTVEgdRs。
发布于 2019-04-05 23:32:20
通常,ifelse在相同的数据帧中运行,确保不跨数据集的行数相等。先考虑合并,然后运行ifelse。下面运行一个左联接合并,保留atbat中的所有行。
merged_df <- merge(atbat, retro_games,
by.x = c("date", "pitcher_name"),
by.y = c("Date", "HmStPchNm"), all.x = TRUE)
merged_df$starting_catcher_retro = with(merged_df,
ifelse((num == 1)
& (inning_side == "top")
& (HmBat1Pos == 2),
HmBat1ID, "no")
)https://stackoverflow.com/questions/55543824
复制相似问题