我必須計算一場比賽中不同球員的加權分數。我在前三欄有他們的個人得分,在后三欄有他們的權重,這些權重因游戲而異(每行代表一個游戲)。
誰能幫助我以一種有效的方式計算加權分數?
df <- data. frame(player1 = c(1。 2。 1)。 player2 = c(2, 2。 1)。 player3 = c(2。 2,2)。 weightplayer1=c(0. 7,0.8, 0. 7)。 weightplayer2 = c(/span>0. 6,0.1, 0. 6), weightplayer3=c(0。 2,0.7, 0. 2))
# player1 player2 player3 weightplayer1 weightplayer2 weightplayer3
# 1 2 2 0.7 0.6 0.2
# 2 2 2 0.8 0.1 0.7
# 1 1 2 0.7 0.6 0.2。
我需要一個這樣的輸出,其中weighted1scores列取的是得1分的球員的權重之和 得分1的球員的權重之和,而weighted2scores列則是得分2的球員的權重之和。 我實際上有一個很長的可能得分的串列,所以實際上這個資料框架有很多內容。 這個資料框架有很多列(包括直到加權100分等)。 因此,一個有效的公式/回圈將是非常好的。
# player1 player2 player3 weightplayer1 weightplayer2 weightplayer3 weighted1scores weighted2scores
# 1 2 2 0.7 0.6 0.2 0.7 0.8
# 2 2 2 0.8 0.1 0.7 0 1.6
# 1 1 2 0.7 0.6 0.2 1.3 0.2
uj5u.com熱心網友回復:
你可以將你的表規范化為3NF,然后進行連接和聚合:
library(tidyverse)
df <- data. frame(player1 = c(1。 2。 1)。 player2 = c(2, 2。 1)。 player3 = c(2, 2, 2)。 weightplayer1 = c(/span>0. 7, 0.8, 0. 7)。 weightplayer2 = c(/span>0. 6, 0.1, 0. 6)/span>。 weightplayer3 = c(/span>0. 2, 0.7, 0. 2))
scores<-
df %>%
as_tibble(rownames = "game") %>%
pivot_longer(starts_with("player")) %> %
轉化()
游戲,
player_id = name %> % str_extract("[0-9] $"),
分數 = 值
)
分數
#> # A tibble: 9 × 3
#> game player_id score。
#> <chr> <chr> <dbl>/span>
#> 1 1 1 1
#> 2 1 2 2
#> 3 1 3 2
#> 4 2 1 2
#> 5 2 2 2
#> 6 2 3 2
#> 7 3 1 1
#> 8 3 2 1
#> 9 3 3 2
權重 <-
df %>%
as_tibble(rownames = "game") %>%
pivot_longer(starts_with("weight")) %> %
轉化()
游戲,
player_id = name %> % str_extract("[0-9] $"),
weight = value
)
權重
#> # A tibble: 9 × 3
#> game player_id weight
#> <chr> <chr> <dbl>/span>
#> 1 1 1 0.7
#> 2 1 2 0.6
#> 3 1 3 0.2
#> 4 2 1 0.8
#> 5 2 2 0.1
#> 6 2 3 0.7
#> 7 3 1 0.7
#> 8 3 2 0.6
#> 9 3 3 0.2
分數%>%
inner_join(weights) %> %
group_by(player_id, game) %>%
summarise(加權= sum(weight)) %> %
pivot_wider()
names_from = player_id,
values_from =weighted,
names_prefix = "weighted"/span>
)
#> Joining, by = c("game", "player_id")
#> `summarise()`已經按'player_id'分組輸出。你可以使用".groups "引數來覆寫。
#> # A tibble: 3 × 4
#> game weighted1 weighted2 weighted3
#> <chr> <dbl> <dbl> <dbl>/span>
#> 1 1 0.7 0.6 0.2
#> 2 2 0.8 0.1 0.7
#> 3 3 0.7 0.6 0.2
創建于2021-09-20,由reprex包(v2.0.1)
uj5u.com熱心網友回復:
這里有一個方法:
df %>%
mutate(game = row_number()) %> %
pivot_longer(cols = starts_with('player')。 names_to = "Player"。 values_to = "Score") %> %
pivot_longer(cols = starts_with('weightplayer')。 names_to = "WPlayer"。 values_to = "Weight") %> %
filter(parse_number(Player) == parse_number(WPlayer) ) %> %
select(-WPlayer) %> %
mutate()
WeightedScore = Score * Weight
)
你可以保持原樣,以回傳這個整潔的表格
。# A tibble: 9 x 5
游戲 球員得分 權重 加權得分
<int> <chr> < dbl> <dbl> > < dbl>
1 1 player1 1 0.7 0.7
2 1 players2 2 0.6 1.2
3 1 player3 2 0.2 0.4
4 2 players1 2 0.8 1.6
5 2 players2 2 0.1 0.2
6 2 players3 2 0.7 1.4
7 3 players1 1 0.7 0.7
8 3 player21 0.6 0.6
9 3 player3 2 0.2 0.4
或者繼續說:
df %>%
mutate(game = row_number()) %> %
pivot_longer(cols = starts_with('player')。 names_to = "Player"。 values_to = "Score") %> %
pivot_longer(cols = starts_with('weightplayer')。 names_to = "WPlayer"。 values_to = "Weight") %> %
filter(parse_number(Player) == parse_number(WPlayer) ) %> %
select(-WPlayer) %> %
mutate()
WeightedScore = Score * Weight
) %>%
pivot_longer(cols = c(Score, Weight, WeightedScore) %> %
mutate(name = paste(Player, name。 sep = '_') %> %
pivot_wider(id = game)
結束于:
# A tibble: 3 x 10
游戲 player1_Score player1_Weight player1_WeightedScore player2_Score player2_Weight player2_WeightedScore player3_Score player3_Weight player3_WeightedScore
<int> <dbl> < dbl> <dbl> > <dbl> <dbl> < <。 dbl> <dbl> < dbl>
1 1 1 0.7 0。 7 2 0.6 1。 2 2 0.2 0.4
2 2 2 0.8 1. 6 2 0.1 0。 2 2 0.7 1.4
3 3 1 0.7 0. 7 1 0.6 0。 6 2 0.2 0.4
uj5u.com熱心網友回復:
到目前為止,其他的解決方案沒有重現你的輸出,可能是因為你沒有按照我們期望的方式使用 "權重"。
下面的方法就可以做到:
df <- data. frame(player1 = c(1。 2。 1)。 player2 = c(2, 2。 1)。 player3 = c(2。 2,2)。 weightplayer1=c(0. 7,0.8, 0. 7)。 weightplayer2 = c(/span>0. 6,0.1, 0. 6), weightplayer3=c(0。 2,0.7, 0. 2))
library(tidyverse)
df <- df %> % mutate(game = row_number())
df %>%
pivot_longer()
cols = player1:weightplayer3,
names_to = c(" 。 value", "player_id"),
names_pattern = "(. )(d)") %>%
重命名(score = player,weight = weightplayer, 玩家= player_id) %> %
group_by(game, score_col = paste0("weighted"/span>, score,"score") %> %
summarize(weightedscore = sum(重量)) %> %
pivot_wider(names_from = score_col, values_from = weightedscore, values_fill = 0) %> %
left_join(df,.) %>%
select(-game) %>%
as.data.frame() # just to print all columns。
#> `summarise()`已經按'游戲'分組輸出。你可以使用".groups "引數來覆寫它。
#> Joining, by = "game"
#> player1 player2 player3 weightplayer1 weightplayer2 weightplayer3
#> 1 1 2 2 0.7 0.6 0.2
#> 2 2 2 2 0.8 0.1 0.7
#> 3 1 1 2 0.7 0.6 0.2
#> weighted1score weighted2score
#> 1 0.7 0.8
#> 2 0.0 1.6
#> 3 1.3 0.2。
創建于2021-09-20,由reprex包(v2.0.1)
我們首先將資料重塑為整齊的資料(每行一個觀察值),在報告之前,我們最好一直呆在這里,然后我們進行匯總計算,將其重塑為不整齊的資料,并將其縫合到原始資料框中。
uj5u.com熱心網友回復:
下面是另一種方法,它可能更簡潔一些:
library(tidyverse)
df %>%。
mutate(output = pmap(. , ~ {x < - c(. .. )[startsWith(names(df)。 "player")]
y <- c(... )[startsWith(names(df)。 "weight")]
as_tibble(cbind(sum(y[x == 1])。 sum(y[/span>x == 2]))))) 標點符號">})) %> %
unnest_wider(output) %> %
rename_with(~ gsub("V(d )"。 "Weighted1scores", 。 )。 starts_with("V")).
# A tibble: 3 x 8
玩家1 玩家2 玩家3 權重 玩家1 權重 玩家2 權重 玩家3 權重1scores
<dbl> <dbl> <dbl> < dbl> <dbl> < dbl> <dbl>
1 1 2 2 0。 7 0.6 0.2 0.7
2 2 2 0. 8 0.1 0.7 0
3 1 1 2 0。 7 0.6 0.2 1.3
# ... with 1 more variable: Weighted2scores <dbl>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/326395.html
標籤:
上一篇:在r資料框中查找資料集中的佇列
