0

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}
cderv
  • 6,272
  • 1
  • 21
  • 31
Eric Carey
  • 13
  • 1
  • 2
    It's easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data and the desired output for that input. But looks like you might just want `Profit.flag <- (S2$Profit>=0) + 0` – MrFlick Oct 02 '17 at 20:19

2 Answers2

1

Try this:

require(dplyr) 
df<- df %>%
         mutate(flag= ifelse(column>=0, 1, 0))

where df is your data frame and column- column name for which you want to check the value

1

You do not need loop here. This should work:

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

or

Profit.flag <- as.integer(Profit[,1] >= 0)
djhurio
  • 5,437
  • 4
  • 27
  • 48