if_else不按我想要的方式工作

时间:2018-02-13 03:45:05

标签: r if-statement

我在这里有这个数据框:

structure(list(period = c(1, 1, 1, 1, 1, 1), time = structure(c(-2209006260, 
-2209010160, -2209012620, -2209014540, -2209016580, -2209018140
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), strength = c("5v5", 
"5v5", "5v5", "5v5", "5v5", "5v5"), team = c("TOR", "TOR", "TOR", 
"MTL", "MTL", "MTL"), shooter = c(28, 36, 19, 67, 13, 15), shot_type = c("W", 
"W", "T", "W", "S", "W"), a1 = c(21, 24, 3, 76, 81, 22), a2 = c(3, 
NA, 42, NA, 13, NA), a3 = c(2, NA, NA, NA, NA, NA), a1_zone = c("nl", 
"of", "ol", "dsl", "or", "nc"), a2_zone = c("dsc", NA, "ofp", 
NA, "or", NA), a3_zone = c("dl", NA, NA, NA, NA, NA), scoring_chance = c(NA, 
NA, NA, "Y", NA, "Y"), shots_on_goal = c("Y", NA, NA, "Y", NA, 
NA), odd_man = c(NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_), goals = c(NA, NA, 
NA, "Y", NA, NA), rebound = c(NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_), rebound_sog = c(NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_), 
    rebound_goals = c(NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_), home_score_state = c(0, 
    0, 0, 0, -1, -1), goalie = c(31, 31, 31, 45, 45, 45), game_id = c(20001, 
    20001, 20001, 20001, 20001, 20001), date = structure(c(1444176000, 
    1444176000, 1444176000, 1444176000, 1444176000, 1444176000
    ), class = c("POSIXct", "POSIXt"), tzone = "UTC"), home_team = c("TOR", 
    "TOR", "TOR", "TOR", "TOR", "TOR"), away_team = c("MTL", 
    "MTL", "MTL", "MTL", "MTL", "MTL")), .Names = c("period", 
"time", "strength", "team", "shooter", "shot_type", "a1", "a2", 
"a3", "a1_zone", "a2_zone", "a3_zone", "scoring_chance", "shots_on_goal", 
"odd_man", "goals", "rebound", "rebound_sog", "rebound_goals", 
"home_score_state", "goalie", "game_id", "date", "home_team", 
"away_team"), row.names = c(NA, -6L), class = c("tbl_df", "tbl", 
"data.frame"))

我想要的是使用if_else更改列:scoring_chanceshots_on_goalgoalsrebound_sogrebound_goals,以便在列中有NA值,它将变为0,否则,将其变为1.

我拥有的是:

pass %>% 
  mutate(scoring_chance = if_else(scoring_chance == "NA", "0", "1"),
         shots_on_goal = if_else(shots_on_goal == "NA", "0", "1"),
         goals = if_else(goals == "NA", "0", "1"),
         rebound_sog = if_else(rebound_sog == "NA", "0", "1"),
         rebound_goals = if_else(rebound_goals == "NA", "0", "1")
  ) 

这会将非NA的所有内容转换为1,但仍会保留NA值......

1 个答案:

答案 0 :(得分:4)

我想你想要<?php // Start the session session_start(); // call the username echo $_SESSION['uname']; ?> is.na()与字符串“NA”进行比较,而不是缺失值NA。

== "NA"