如何在R中的ifelse语句中写入多个条件?

时间:2018-11-18 19:10:52

标签: r

我有三列,即Team1,Team2和Result。以下是数据示例。我需要一列“获胜/亏损”列,以便如果结果出现在第1队或第2队中,那么它应该是赢家,否则应该是输家。

Team1        Team2             Result      Winning/lossing team
Pakistan       NA             Australia        lossing
Australia      Australia      Australia        winning
Australia      NA             Australia         winning

The below is the code whuch I tried ,but its not working.Please help.
Playerwnorloss=ifelse(Dataset$Team1==Dataset$winner | is.na(Dataset$Team2==Dataset$winner),"WinningTeam","LossingTeam")

2 个答案:

答案 0 :(得分:1)

那又怎么样:

Dataset$WinningLosing <- ifelse(Dataset$Result == Dataset$Team1 & !is.na(Dataset$Team1) | Dataset$Result == Dataset$Team2 & !is.na(Dataset$Team2), "Winning","Losing")

输出:

       Team1     Team2    Result WinningLosing
1:  Pakistan      <NA> Australia        Losing
2: Australia Australia Australia       Winning
3: Australia      <NA> Australia       Winning

答案 1 :(得分:0)

尝试一下(假设您的数据在df中):

   <body>

   </body>
   <div class="pimg3">
        <div class="row">
        <div class="ptext">
                <h1><strong>HOW I WORK</strong></h1>
                <div class="column">
                    <i class="fas fa-search" style="color:#1d5ba0; padding: 10px"></i>
                    <h2>Research</h2>
                    <p>Understand the problem, empathize with the person. Explore the competitive landscape. 
                        Identify strategic opportunities. Define design goals/requirements.</p>
                </div>
                <div class="column">
                    <i class="fas fa-flask" style="color:#1d5ba0; padding: 10px"></i>
                    <h2>Design</h2>
                    <p>Combine effective interfaces, smart interactions, and consistent visual language into
                         a captivating design that resonates with the target audience.
                        </p>
                </div>
                <div class="column">
                    <i class="fas fa-code" style="color:#1d5ba0; padding: 10px"></i>
                    <h2>Build</h2>
                    <p>Map design to code. Smooth constraints. Solve edge cases. Move that button 3 pixels to the left. Fill 
                        the gap between functional and delightful.</p>
                </div>
                <div class="column">
                    <i class="fas fa-retweet" style="color:#1d5ba0; padding: 10px"></i>
                    <h2>Refine </h2>
                    <p>Test prototypes with real people against actual problems. Combine insights with gut intuition and experience. 
                        Iterate and improve in pursuit of perfection.</p>
                </div>
            </div>
        </div>
    </div>        
    </body>

问候 Paweł