简体   繁体   English

如何根据另一列的条件创建新列,但输出超出二进制值

[英]How to create a new column based on condition from another column, but with outputs beyond binary values

So, I have a tool data on some basic football players' statistics.所以,我有一个关于一些基本足球运动员统计数据的工具数据。 I want to classify leagues based on their difficulty between 1 and 0.4.我想根据 1 到 0.4 之间的难度对联赛进行分类。 If I use ifelse statement, I believe I will get binary responses, and I want, at least, 4 "levels" on this new column (League difficulty).如果我使用ifelse语句,我相信我会得到二进制响应,并且我希望在这个新列(联盟难度)上至少有 4 个“级别”。 How could I do that?我怎么能那样做?

Data example:数据示例:

 Player            Season Team             League       Goals
   <chr>             <chr>  <chr>            <chr>        <dbl>
 1 L. Messi          14-15  Barcelona        La Liga         59
 2 Luis Suárez       14-15  Barcelona        La Liga         19
 3 Neymar            14-15  Barcelona        La Liga         41
 4 A. Griezmann      14-15  Atletico         La Liga         24
 5 Cristiano Ronaldo 14-15  Real Madrid      La Liga         61
 6 M. Antonio        14-15  Nottingham       Championship    15
 7 K. Benzema        14-15  Real Madrid      La Liga         17
 8 G. Bale           14-15  Real Madrid      La Liga         13
 9 A. Adúriz         14-15  Athletic         La Liga         23
10 C. Bacca          14-15  Sevilla          La Liga         28
11 Iago Aspas        14-15  Sevilla          La Liga          7
12 P. Alcacér        14-15  Valencia         La Liga         13
13 M. Götze          14-15  Bayern           Bundesliga      16
14 R. Lewandowski    14-15  Bayern           Bundesliga      24
15 A. Robben         14-15  Bayern           Bundesliga      16
16 Thomas Müller     14-15  Bayern           Bundesliga      20
17 T. Werner         14-15  Augsburg         Bundesliga       8
18 M. Kruse          14-15  Monchengladbach  Bundesliga      11
19 L. Stindl         14-15  Monchengladbach  Bundesliga      10
20 I. Perisic        14-15  Wolfsburg        Bundesliga       9
21 B. Dost           14-15  Wolfsburg        Bundesliga      19
22 K. De Bruyne      14-15  Wolfsburg        Bundesliga      17
23 K. Volland        14-15  Hoffenheim       Bundesliga      10
24 Roberto Firmino   14-15  Hoffenheim       Bundesliga      12
25 Heng Min Son      14-15  Bayer Leverkusen Bundesliga      12
26 L. Sané           14-15  Schalke          Bundesliga       4
27 P. Aubameyang     14-15  Borussia         Bundesliga      22
28 M. Reus           14-15  Borussia         Bundesliga      11
29 C. Immobile       14-15  Borussia         Bundesliga      10
30 Hulk              14-15  Zenit            Russian PL      18
31 S. Rondón         14-15  Zenit            Russian PL      18
32 S. Azmoun         14-15  Rubin            Russian PL       2
33 A. Dzyuba         14-15  Spartak          Russian PL       5
34 M. Icardi         14-15  Inter            Serie A         26
35 M. Hamsik         14-15  Napoli           Serie A         14
36 D. Mertens        14-15  Napoli           Serie A         11
37 Jorginho          14-15  Napoli           Serie A          1
38 G. Higuaín        14-15  Napoli           Serie A         26
39 D. Zapata         14-15  Napoli           Serie A          8
40 L. Insigne        14-15  Napoli           Serie A          1
41 L. Muriel         14-15  Sampdoria        Serie A          4
42 Bruno Fernandes   14-15  Udinese          Serie A          3
43 P. Dybala         14-15  Palermo          Serie A         14
44 M. Salah          14-15  Fiorentina       Serie A          8
45 J. Ilicic         14-15  Atalanta         Serie A          9
46 D. Berardi        14-15  Sassuolo         Serie A         15
47 João Pedro        14-15  Cagliari         Serie A          3
48 A. Candreva       14-15  Lazio            Serie A         12
49 F. Quagliarella   14-15  Torino           Serie A         14
50 A. Morata         14-15  Juventus         Serie A         16
51 P. Pogba          14-15  Juventus         Serie A         12
52 K. Coman          14-15  Juventus         Serie A          1

So, let's say I want a level for Serie A, another for Premier League, another for La Liga and another for Bundesliga.所以,假设我想要意甲联赛的一个级别,英超联赛的另一个级别,西甲联赛的另一个级别和德甲联赛的另一个级别。

You could use several chained ifelse to do that;您可以使用多个链接ifelse来做到这一点; however, it might be easier to do with case_when .但是,使用case_when可能更容易。

library(dplyr)

df |>
  mutate(score = case_when(League == "La Liga" ~ 0.6,
                           League == "Championship" ~ 0.9,
                           League == "Bundesliga" ~ 0.8,
                           League == "Russian PL" ~ 0.7,
                           League == "Serie A" ~ 0.5))

#    Player Season       Team       League Goals score
#1   L.Messi  14-15  Barcelona       La Liga    59   0.6
#2 M.Antonio  14-15 Nottingham Championship    15   0.9
#3   M.Götze  14-15     Bayern   Bundes liga    16   0.8
#4      Hulk  14-15      Zenit    Russian PL    18   0.7
#5  M.Hamsik  14-15     Napoli       Serie A    14   0.5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM