Funrab Funrab - 10 days ago 5
R Question

Warning message: In if (data$body > 0) { : the condition has length > 1 and only the first element will be used

I immediately apologize for the fact that many have already asked it.

I need your help, guys.

I have data.frame, which has a 'body' column. If body> 0, we compute the two columns on the same rules if the 'body' <0, then by different rules.

To bring the concept after a troubled part of my code:

if(data$body > 0){
data$shadow.up <- data$High - data$Close
data$shadow.down <- data$Open - data$Low
}else{
data$shadow.up <- data$High - data$Open
data$shadow.down <- data$Close - data$Low
}

Answer

You want to try the following solution:

data$shadow.up <- ifelse(data$body > 0, data$High - data$Close, data$High - data$Open) data$shadow.down <- ifelse(data$body > 0, data$Open - data$Low, data$Close - data$Low)

In your solution the if condition gets a vector of logicals and it will use only the first value in that vector.