0

文字ベクトルの形式でいくつかの野球のバッティング ラインナップを抽出しました。

[1] "Yunel Escobar"     "Kelly Johnson"     "Jose Bautista"     "Adam Lind"        
[5] "Edwin Encarnacion" "Brett Lawrie"      "Eric Thames"       "Colby Rasmus"     
[9] "Jeff Mathis"

162試合シーズンのすべての試合の打順をリストするデータフレームallLineupsをRで作成しました

ヘッド(全ラインナップ)

player          order game
 Yunel Escobar     1    1
 Kelly Johnson     2    1
 Jose Bautista     3    1
 Adam Lind         4    1
 Edwin Encarnacion 5    1
 Brett Lawrie      6    1

私は今、以下を含むいくつかの分析をしたいと思います

a) シーズン中、特定の 9 人のプレーヤーのグループがバッティング ラインナップに含まれる頻度

b) まったく同じラインナップ (順序どおり) は何回発生しますか?

c) 指定された 2 人のプレイヤーが一緒に出現する頻度

d) 特定のゲームについて、そのラインナップは、たとえば最初のゲームのラインナップとどのように比較されますか?

これらのクエリに回答する方法についてのガイダンスをいただければ幸いです

4

1 に答える 1

2

sort以下のコメントで、OPが要求しているものを配信するための呼び出しを追加します。

player <- c("Yunel Escobar"  ,   "Kelly Johnson"  ,   "Jose Bautista"   ,  "Adam Lind"   ,     
"Edwin Encarnacion", "Brett Lawrie"   ,   "Eric Thames"   ,    "Colby Rasmus"    , 
"Jeff Mathis")

# create two games with different lineups
allLineups <- data.frame(player=c(player, rev(player)) , order=1:9, game=rep(1:2, each=9))

#construct a lineup
with(allLineups, tapply(player, game, function(x) paste0(sort(x), collapse="/") ) )

# tabulate the values for lineups
table( with(allLineups, tapply(player, game, function(x) paste0(sort(x), collapse="/") ) ) )

以下を使用して、ラインナップリストを短縮できます。

allLineups$shortplyr <- sub("^(.).+\\ (.{4}).*$", '\\1_\\2', allLineups$player)
# ------------
table( with(allLineups, tapply(shortplyr, game, function(x) paste0(sort(x), collapse="/") ) ) )

A_Lind/B_Lawr/C_Rasm/E_Enca/E_Tham/J_Baut/J_Math/K_John/Y_Esco 
                                                             2 

そして、OPは明らかにこれを望んでいません。

また、ラインナップが注文されていない場合は、次のように並べ替える必要があります。

allLineups <- with( allLinups, allLineups[ order(game, order) , ]
于 2012-08-08T02:16:01.623 に答える