R中的多个If语句 [英] Multiple If Statements in R
本文介绍了R中的多个If语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直试图解决这一问题,但无济于事. 我有一个if语句,旨在满足四个可能的条件.
I've been trying to figure this out all day but to no avail. I have an if statement that is meant to satisfy four possible conditions.
- A存在而B不存在
- B存在而A不存在
- A& B存在
- A& B不存在
A,B,C是数据帧.
A, B, C are dataframes.
这是我的代码:
if (!exists("A") & exists("B")) {
C= B}
else if (exists("A") & !exists("B")) {
C= A}
else if (exists("A") & exists("B")) {
C= rbind(B,A)}
else {C <- NULL}
我总是在意外的}"和意外的"else"上收到错误消息. 我已经举了几个例子,但是仍然面临着挑战.任何指针将不胜感激.谢谢.
I keep getting an error on unexpected "}" and unexpected "else". I've followed several examples but still facing this challenge. Any pointers would be much appreciated. Thx.
推荐答案
尝试一下
if (!exists("A") & exists("B")) {
C= B
} else if (exists("A") & !exists("B")) {
C= A
} else if (exists("A") & exists("B")) {
C= rbind(B,A)
} else {C <- NULL}
这篇关于R中的多个If语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文