本文介绍了R中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)

这有什么问题?

问题:


  1. 为什么2个版本的代码无效?

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

if(){} ifelse()功能有什么区别在R?

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

ifelse()中,如果条件为为真,如何进行多项操作?

示例: ifelse(categorical_preds ==setosa,数据$ 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

告诉你在如果条件相当于使用其第一个元素:

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
}

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

10-18 18:01