if()和ifelse()函数之间的区别 [英] Difference between if() and ifelse() functions
问题描述
我想虚拟代码,即为种类"列创建标志变量.
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?
-
为什么2个版本的代码都不起作用?
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屋!