Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
465 views
in Technique[技术] by (71.8m points)

r - Difference between if() and ifelse() functions

I want to dummy code i.e. create flag variables for column Species.

I wrote the below code:

create_dummies <- function(data, categorical_preds){
    if (categorical_preds == "setosa"){data$setosa_flg <- 1}
    else {data$setosa_flg <- 0}
    if (categorical_preds == "versicolor"){data$versicolor_flg <- 1}
    else {data$versicolor_flg <- 0}
    if (categorical_preds == "virginica"){data$virginica_flg <- 1}
    else {data$virginica_flg <- 0}
    return(data)
}
create_dummies(iris,iris$Species)

I got a warning:

Warning messages:
1: In if (categorical_preds == "setosa") { :
  the condition has length > 1 and only the first element will be used
2: In if (categorical_preds == "versicolor") { :
  the condition has length > 1 and only the first element will be used
3: In if (categorical_preds == "virginica") { :
  the condition has length > 1 and only the first element will be used

Then I changed the code to:

create_dummies <- function(data, categorical_preds){
    ifelse(categorical_preds == "setosa",data$setosa_flg <- 1,data$setosa_flg <- 0)
    ifelse(categorical_preds == "versicolor",data$versicolor_flg <- 1,data$versicolor_flg <- 0)
    ifelse(categorical_preds == "virginica",data$virginica_flg <- 1,data$virginica_flg <- 0)

    return(data)
}
create_dummies(iris,iris$Species)

No warning this time but the new dummy variables are always 0.

As a next step I want to avoid hardcoding so i wrote

create_dummies <- function(data, categorical_preds){
catvar <- (unique(categorical_preds))

for (i in 1:length(catvar)){
  iris[catvar[i]] <- ifelse(iris$Species == catvar[i],1,0)
}
return(data)
}
create_dummies(iris,iris$Species)

What is wrong with this?

Questions:

  1. Why the 2 versions of the code is not working?

  2. What is difference between if(){} and ifelse() function in R?

  3. In ifelse(), if the condition is true, how can I do multiple action?
    example: ifelse(categorical_preds == "setosa",data$setosa_flg <- 1 print(iris$Species),data$setosa_flg <- 0).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The warning message:

  the condition has length > 1 and only the first element will be used

tells you that using a vector in if condition is equivalent to use its first element :

[if (v == 1)] ~ [if (v[1] == 1)] ## v here is a vector

You should use the vectorized ifelse. For example you can write your condition like this:

create_dummies<-function(data, categorical_preds){
  ## here I show only the first condition 
  data$setosa_flg <-
       ifelse (categorical_preds=="setosa",1,0)
  data
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...