0

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 
}
Funrab
  • 83
  • 1
  • 10
  • data$body is a vector, therefore it is saying 'the condition has length > 1" and will only use the first element of the vector to be used in the if statement – Ansjovis86 Nov 29 '16 at 09:26

1 Answers1

0

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.

Jim Raynor
  • 198
  • 2
  • 9