Eric Carey Eric Carey - 3 years ago 164
R Question

Creating Flag Variable based on negativity/positivity in R

I am attempting to create a flag variable called Profit.flag that indicates whether a specific column in my dataset is profitable (>= 0) or not (<0). This is currently what I have but I am getting an error. Any help would be much obliged!

Profit = data.frame(S2$Profit)
for(i in Profit)
{if (Profit[i] >= 0)
{Profit.flag[i] == 1}
else Profit.flag[i] == 0}

Answer Source

You do not need loop here. This should work:

Profit.flag <- as.integer(S2$Profit >= 0)


Profit.flag <- as.integer(Profit[,1] >= 0)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download