if() 和 ifelse() 函数的区别 [英] Difference between if() and ifelse() functions

查看:37
本文介绍了if() 和 ifelse() 函数的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想虚拟代码,即为列 Species 创建标志变量.

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

我写了下面的代码:

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)

我收到警告:

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

然后我把代码改成:

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)

这次没有警告,但新的虚拟变量始终为 0.

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?

  1. 为什么 2 个版本的代码不起作用?

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

R 中的 if(){}ifelse() 函数有什么区别?

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

ifelse()中,如果条件为true,我如何做多个动作?
示例:ifelse(categorical_preds == "setosa",data$setosa_flg <- 1 print(iris$Species),data$setosa_flg <- 0).

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).

推荐答案

警告信息:

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

告诉你在 if 条件中使用向量等同于使用它的第一个元素:

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

您应该使用矢量化的 ifelse.例如你可以这样写你的条件:

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
}

这篇关于if() 和 ifelse() 函数的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆