我可以掃描表格列表以獲得條件概率還是必須單獨掃描每個表格?這裏有一些數據我編造的,與表的輸出一起,向人們展示我的問題多個表格按列總和劃分表格元素
positions <- c("forward", "midfield", "defence", "goalkeeper", rep("forward", 5), "defender")
nationality <- c(rep("IRE", 3), "GER", "ITA", "ENG", "FRA", "BRA", "SCO", "ESP")
teams <- c("Milan", "Juventus", "Lazio", "Palermo", "Milan", "Juventus", "Chievo", "Siena", "Parma", "Napoli")
dF <- data.frame(Team = factor(teams),
Position = factor(positions),
Nationality = factor(nationality),
stringsAsFactors = T)
t1 <- t(table(dF$Nationality, dF$Position))
t2 <- t(table(dF$Nationality, dF$Team))
BRA ENG ESP FRA GER IRE ITA SCO
defence 0 0 0 0 0 1 0 0
defender 0 0 1 0 0 0 0 0
forward 1 1 0 1 0 1 1 1
goalkeeper 0 0 0 0 1 0 0 0
midfield 0 0 0 0 0 1 0 0
BRA ENG ESP FRA GER IRE ITA SCO
Chievo 0 0 0 1 0 0 0 0
Juventus 0 1 0 0 0 1 0 0
Lazio 0 0 0 0 0 1 0 0
Milan 0 0 0 0 0 1 1 0
Napoli 0 0 1 0 0 0 0 0
Palermo 0 0 0 0 1 0 0 0
Parma 0 0 0 0 0 0 0 1
Siena 1 0 0 0 0 0 0 0
我需要做的是通過劃分其列的總和每個表項,以獲得條件概率。我知道我可以使用應用於每個表的掃描函數,但我想知道它是否可以通過lapply或通過for循環應用於表列表?我嘗試過都沒有成功。我也嘗試過沒有成功的掃描功能。
例如:
tbl_list <- list(t1, t2)
for(tbl in tbl_list) {
tbl <- sweep(tbl, 2, colSums(tbl), `/`)
}
它很好用,謝謝。你在那裏定義的兩個函數究竟發生了什麼? – Seanosapien