Eric Carey Eric Carey - 1 year ago 102
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)
Profit.flag=numeric(474)
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)

or

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