0
library(quantmod)
getSymbols("LLOY.L",
           from = datefrom,
           to = dateto,
           auto.assign = TRUE)
LLOY.L$MovingAverages <- 0
e <- movavg(LLOY.L$LLOY.L.Close, 5, type = c("e"))
LLOY.L[, 7] <- e

Is there a way to create a new column of this dataset that means if column 4 is greater than column 7 it prints "Buy Stock" in each row, else print "Sell Stock", I have tried using loops with no success

zelite
  • 1,478
  • 16
  • 37
  • Please add a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). Also show what you have already tried.That way you can help others to help you! – dario Feb 14 '20 at 10:33

1 Answers1

1

You can easily do it using ifelse function in R.

df$new_var <- ifelse(df[, 4] > df[, 7], "Buy Stock", "Sell Stock")

where df is your dataset, in which you want create new column.

I hopes, it helps you.

Neeraj
  • 1,166
  • 9
  • 21