R:使用ifelse在数据框上运行多个逻辑测试以创建新列时遇到麻烦

时间:2019-04-05 21:53:50

标签: r dataframe if-statement match

我试图在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

1 个答案:

答案 0 :(得分:1)

通常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")
                                     )