簡體   English   中英

R:使用ifelse在數據框上運行多個邏輯測試以創建新列時遇到麻煩

[英]R: Trouble using ifelse to run multiple logical tests across dataframes to create new column

我正在嘗試在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排的兩個dataframes樣品太長,包括在這里就是一個引擎收錄- https://pastebin.com/kTVEgdRs

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM