【发布时间】:2013-07-01 10:54:39
【问题描述】:
我有一个名为newdata 的数据框。它有两列名为BONUS 和GENDER。
当我在r 中编写以下代码时:
> newdata <- within(newdata,{
PROMOTION=ifelse(BONUS>=1500,1,0)})
虽然我没有在这里使用循环,但它可以工作,但以下代码在没有循环的情况下不起作用。为什么?
> add <- with(newdata,
if(GENDER==F)sum(PROMOTION))
Warning message:
In if (GENDER == F) sum(PROMOTION) :
the condition has length > 1 and only the first element will be used
我的问题是为什么在第一个代码中所有元素都被使用了?
【问题讨论】:
-
这在here之前已经问过了
-
This is a very interesting post on
if-else,如果您有兴趣了解更多信息。
标签: r loops if-statement for-loop