简体   繁体   中英

Add new column in a dataset based on dataset

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM