0

I am using the formula below to calculate a vector but when executed, it returns a matrix; here is the code:

FdeValue <- ifelse(fde.4$ResourceId == 2196 |fde.4$FullYearForecast == 'Yes' | !is.na(fde.2$8), 0, fde.4$Value)

Any idea why will it do that ?

agenis
  • 8,069
  • 5
  • 53
  • 102
Fahadakbar
  • 488
  • 1
  • 8
  • 26
  • Can you post data for `fde.4` ? – Tim Biegeleisen Oct 19 '18 at 13:49
  • Please make your example reproducible! Read https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – jogo Oct 19 '18 at 13:50
  • unfortunately , i cant upload the data. Problem i see is that the heading/column name of the resulting vector is the column name used in the third condition, which is really weird – Fahadakbar Oct 19 '18 at 13:57
  • 1
    At least show us the `str` of the data then. Also, a non-standard column name (here, `8`) shouldn't be able to be used with the `$` operator; is this code actually running? – Aaron left Stack Overflow Oct 19 '18 at 14:01

1 Answers1

0

In my opinion it is because of the dim of your logical input. Ifelse returns a value of the same dim as your logical input.
Try this:

c(fde.4$ResourceId ,fde.4$FullYearForecast, is.na(fde.2$8))==c(2196,'Yes', FALSE)
jogo
  • 12,469
  • 11
  • 37
  • 42
T. Ciffréo
  • 126
  • 10
  • Try this: c(fde.4$ResourceId ,fde.4$FullYearForecast, is.na(fde.2$8))==c(2196,'Yes', FALSE) – T. Ciffréo Oct 19 '18 at 13:59
  • awesome, it worked, but i still do not understand why it happened , i am using same formula for another vector and that is working fine, why would ifelse return the same dimension as logical input ? – Fahadakbar Oct 19 '18 at 14:43
  • 1
    Ifelse checks every single element of your logical vector and do not see it as an unique assumption. Therefore for every 'TRUE' or 'FALSE' it returns the output you indicate. – T. Ciffréo Oct 19 '18 at 15:18
  • 1
    It is applied element by element so if you apply an operation on a vector of length n or a matrix of dim n*m you get an output of respectively length n or dim n*m – T. Ciffréo Oct 19 '18 at 15:19