嵌套的ifelse语句 [英] Nested ifelse statement
问题描述
我还在学习如何将SAS代码翻译成R,然后收到警告。我需要了解我犯错误的地方。我想要做的是创建一个变量来总结和区分人口的3种状态:大陆,海外,外国人。
我有一个包含2个变量的数据库:
I'm still learning how to translate a SAS code into R and I get warnings. I need to understand where I'm making mistakes. What I want to do is create a variable which summarizes and differentiates 3 status of a population: mainland, overseas, foreigner. I have a database with 2 variables:
- id国籍:
idnat
(法国人,外国人),
- id nationality:
idnat
(french, foreigner),
如果 idnat
是法国人那么:
- id birthplace:
idbp
(大陆,殖民地,海外)
- id birthplace:
idbp
(mainland, colony, overseas)
我想总结来自 idnat
和 idbp $的信息c $ c>进入名为
的新变量idnat2
:
I want to summarize the info from idnat
and idbp
into a new variable called idnat2
:
- 状态:k(大陆,海外,外国人)
所有这些变量都使用字符类型。
All these variables use "character type".
预计列idnat2中的结果:
Results expected in column idnat2 :
idnat idbp idnat2
1 french mainland mainland
2 french colony overseas
3 french overseas overseas
4 foreign foreign foreign
这是我要在R中翻译的SAS代码:
Here is my SAS code I want to translate in R:
if idnat = "french" then do;
if idbp in ("overseas","colony") then idnat2 = "overseas";
else idnat2 = "mainland";
end;
else idnat2 = "foreigner";
run;
这是我在R中的尝试:
Here is my attempt in R:
if(idnat=="french"){
idnat2 <- "mainland"
} else if(idbp=="overseas"|idbp=="colony"){
idnat2 <- "overseas"
} else {
idnat2 <- "foreigner"
}
我收到此警告:
Warning message:
In if (idnat=="french") { :
the condition has length > 1 and only the first element will be used
我被建议使用嵌套 ifelse
而不是因为它的容易性而是获得更多警告:
I was advised to use a "nested ifelse
" instead for its easiness but get more warnings:
idnat2 <- ifelse (idnat=="french", "mainland",
ifelse (idbp=="overseas"|idbp=="colony", "overseas")
)
else (idnat2 <- "foreigner")
根据警告信息,长度大于1,所以只有在将考虑第一个括号。对不起,但我不明白这个长度与这里有什么关系?有人知道我哪里错了吗?
According to the Warning message, the length is greater than 1 so only what's between the first brackets will be taken into account. Sorry but I don't understand what this length has to do with here? Anybody know where I'm wrong?
推荐答案
如果您使用的是任何电子表格应用程序,那么基本功能 if()
语法:
If you are using any spreadsheet application there is a basic function if()
with syntax:
if(<condition>, <yes>, <no>)
ifelse()<的语法完全相同/ code>在R中:
Syntax is exactly the same for ifelse()
in R:
ifelse(<condition>, <yes>, <no>)
与的唯一区别if()
in电子表格应用程序是R ifelse()
被向量化(将向量作为输入并在输出时返回向量)。考虑以下电子表格应用程序和R中的公式比较,我们希望比较a> b,如果是则返回1,如果不是则返回0。
The only difference to if()
in spreadsheet application is that R ifelse()
is vectorized (takes vectors as input and return vector on output). Consider the following comparison of formulas in spreadsheet application and in R for an example where we would like to compare if a > b and return 1 if yes and 0 if not.
在电子表格中:
A B C
1 3 1 =if(A1 > B1, 1, 0)
2 2 2 =if(A2 > B2, 1, 0)
3 1 3 =if(A3 > B3, 1, 0)
在R中:
> a <- 3:1; b <- 1:3
> ifelse(a > b, 1, 0)
[1] 1 0 0
ifelse()
可以通过多种方式嵌套:
ifelse()
can be nested in many ways:
ifelse(<condition>, <yes>, ifelse(<condition>, <yes>, <no>))
ifelse(<condition>, ifelse(<condition>, <yes>, <no>), <no>)
ifelse(<condition>,
ifelse(<condition>, <yes>, <no>),
ifelse(<condition>, <yes>, <no>)
)
ifelse(<condition>, <yes>,
ifelse(<condition>, <yes>,
ifelse(<condition>, <yes>, <no>)
)
)
要计算列 idnat2
,您可以:
df <- read.table(header=TRUE, text="
idnat idbp idnat2
french mainland mainland
french colony overseas
french overseas overseas
foreign foreign foreign"
)
with(df,
ifelse(idnat=="french",
ifelse(idbp %in% c("overseas","colony"),"overseas","mainland"),"foreign")
)
什么是条件有长度> 1,只使用第一个元素
?让我们看看:
> # What is first condition really testing?
> with(df, idnat=="french")
[1] TRUE TRUE TRUE FALSE
> # This is result of vectorized function - equality of all elements in idnat and
> # string "french" is tested.
> # Vector of logical values is returned (has the same length as idnat)
> df$idnat2 <- with(df,
+ if(idnat=="french"){
+ idnat2 <- "xxx"
+ }
+ )
Warning message:
In if (idnat == "french") { :
the condition has length > 1 and only the first element will be used
> # Note that the first element of comparison is TRUE and that's whay we get:
> df
idnat idbp idnat2
1 french mainland xxx
2 french colony xxx
3 french overseas xxx
4 foreign foreign xxx
> # There is really logic in it, you have to get used to it
我还能使用如果()
?是的,你可以,但语法不是很酷:)
Can I still use if()
? Yes, you can, but the syntax is not so cool :)
test <- function(x) {
if(x=="french") {
"french"
} else{
"not really french"
}
}
apply(array(df[["idnat"]]),MARGIN=1, FUN=test)
如果您熟悉SQL,还可以使用 CASE
sqldf 中的-syntax>声明 包。
If you are familiar with SQL, you can also use CASE
statement in sqldf
package.
这篇关于嵌套的ifelse语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!