在两个或多个条件下的ifelse

时间:2019-02-06 01:42:19

标签: r

如何基于两个列条件在R中使用条件语句来定义列中的值?

数据

import pygame, sys, random, time
screenWidth = 500
gameDisplay = pygame.display.set_mode((screenWidth,screenWidth))
#this begins the game
pygame.init()
#This sets a caption
pygame.display.set_caption("Picnic")

#Variables:
snakeFace = pygame.image.load('morrison.png')
x= 50
y= 50
snakewidth = 30
snakeheight = 30
food1width = 40
food1height = 17
food2width = 25
food2height = 26
food3width = 27
food3height = 23
vel = 5
run = True
lastKey = None
food1 = pygame.image.load("thinMint.png")
food2 = pygame.image.load("cookie.png")
food3 = pygame.image.load("triscuit.png")
randFoodX1 = 0
randFoodY1 = 0
randFoodX2 = 0
randFoodY2 = 0
#randFoodX1 = random.randrange(1, 50, 1) * 10
#randFoodY1 = random.randrange(1, 50, 1) * 10

def generateFoodPosition():
    randFoodX1 = random.randrange(1, 40, 1) * 10
    randFoodY1 = random.randrange(1, 40, 1) * 10
    randFoodX2 = random.randrange(1, 40, 1) * 10
    randFoodY2 = random.randrange(1, 40, 1) * 10
    randFoodX3 = random.randrange(1, 40, 1) * 10
    randFoodY3 = random.randrange(1, 40, 1) * 10
    if randFoodX1 == randFoodX2 or randFoodY1 == randFoodY2 or 
        randFoodY2==randFoodY3 or randFoodY1== randFoodY3 or 
        randFoodX2 == randFoodX3 or randFoodX3 == randFoodX1:
        generateFoodPosition()
    else:
        return [randFoodX1, randFoodY1, randFoodX2, 
                randFoodY2, randFoodX3, randFoodY3]
foodPositions = generateFoodPosition()

def checkForConsumption():
    if ((x + snakewidth) < (foodPositions[0] + food2width + 5 
  and (x + snakewidth) > (foodPositions[0] + food2width - 5)))  or 
((y + snakeheight) < (foodPositions[1] + food2height + 5 and (x + 
snakeheight) > (foodPositions[1] + food2height - 5))):
        foodPositions[0] = random.randrange(1, 40, 1) * 10
    foodPositions[1] = random.randrange(1, 40, 1) * 10
    if ((x + snakewidth) < (foodPositions[2] + food2width + 5 and (x 
+ snakewidth) > (foodPositions[2] + food2width - 5)))  or ((y + 
 snakeheight) < (foodPositions[3] + food2height + 5 and (x + 
 snakeheight) > (foodPositions[3] + food2height - 5))):
    foodPositions[2] = random.randrange(1, 40, 1) * 10
    foodPositions[3] = random.randrange(1, 40, 1) * 10
if ((x + snakewidth) < (foodPositions[4] + food2width + 5 and (x + 
 snakewidth) > (foodPositions[4] + food2width - 5)))  or ((y + 
snakeheight) < (foodPositions[5] + food2height + 5 and (x + 
 snakeheight) > (foodPositions[5] + food2height - 5))):
    foodPositions[4] = random.randrange(1, 40, 1) * 10
    foodPositions[5] = random.randrange(1, 40, 1) * 10

while run:
    pygame.time.delay(10) #1/2 milisecond delay
     #this controls the "X" button
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            lastKey = event.key

#this controls the movement of the snake
if lastKey == pygame.K_LEFT and x > vel:
    x-=vel
if lastKey == pygame.K_RIGHT and x< screenWidth - snakewidth - vel:
    x+=vel
if lastKey == pygame.K_DOWN and y < screenWidth - snakeheight - vel:
    y+= vel
if lastKey == pygame.K_UP and y > vel:
    y-=vel

#if x == (foodPositions[0] or foodPositions[2] or foodPositions[4]) or y== (foodPositions[1] or foodPositions[3] or foodPositions[5]):
checkForConsumption()
gameDisplay.fill((0,0,0))
gameDisplay.blit(snakeFace,(x, y))
gameDisplay.blit(food1, (foodPositions[0], foodPositions[1]))
gameDisplay.blit(food2, (foodPositions[2], foodPositions[3]))
gameDisplay.blit(food3, (foodPositions[4], foodPositions[5]))
pygame.display.update()
print(x)
print(y)


pygame.display.set_mode((screenWidth,screenWidth))
pygame.quit()

伪代码

Term(in month)        DayLate   NEW_STATUS
12                    0         .....
24                    24        .....
17                    30        .....
9                     15        .....
36                    21        .....

1 个答案:

答案 0 :(得分:0)

可以通过在 base 中使用ifelse()或在 dplyr 中使用if_else()case_when()的嵌套条件语句来实现。

# data
df <- structure(list(Term = c(12L, 24L, 17L, 9L, 36L), DayLate = c(0L, 
24L, 30L, 15L, 21L)), class = "data.frame", row.names = c(NA, -5L))

(1)基本方式

within(df,
  NEW_STATUS <- ifelse(Term <= 12,
                       ifelse(DayLate <= 14, "NORM", "SPECIAL"),
                       ifelse(DayLate <= 29, "NORM", "SPECIAL"))
)

(2)dplyr

df %>% mutate(
  NEW_STATUS = case_when(
    Term <= 12 ~ if_else(DayLate <= 14, "NORM", "SPECIAL"),
    TRUE ~ ifelse(DayLate <= 29, "NORM", "SPECIAL")
  )
)

输出

#   Term DayLate NEW_STATUS
# 1   12       0       NORM
# 2   24      24       NORM
# 3   17      30    SPECIAL
# 4    9      15    SPECIAL
# 5   36      21       NORM