I have dataframe with columns
date open high low close adjclose volume
I want to add one more column named "result"(1 if close > open, 0 if close < open)
I do
# Map 1-based optional input ports to variables
data <- maml.mapInputPort(1) # class: data.frame
# calculate pass/fail
data$result <- as.factor(sapply(data$close,function(res)
if (res - data$open >= 0) '1' else '0'))
# Select data.frame to be sent to the output Dataset port
maml.mapOutputPort("data");
But I have only 1 in result. Where is the problem?
The if/else
can return only a single TRUE/FALSE and is not vectorized for length > 1. It may be suitable to use ifelse
(but that is also not required and would be less efficient compared to direct coersion of logical vector to binary ( as.integer
). In the OP's code, the 'close' column elements are looped ( sapply
) and subtracted from the whole 'open' column. The intention might be to do elementwise subtraction. In that case, -
between the columns is much cleaner and efficient (as these operations are vectorized)
data$result <- with(data, factor(as.integer((close - open) >= 0)))
In the above, we get the difference between the columns ('close', 'open'), check if it is greater than or equal to 0 (returns logical vector), convert it to binary ( as.integer
- TRUE -> 1, FALSE -> 0) and then change it to factor
type (if needed)
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.