3

My data.frame looks like this:

  ID Set Choice Apple Microsoft IBM Google Intel HewlettPackard Sony Dell Yahoo Nokia
1  1   1  FALSE     1         0   0      0     0              0    0    0     0     0
2  1   1  FALSE     0         1   0      0     0              0    0    0     0     0
3  1   1  FALSE     0         0   1      0     0              0    0    0     0     0
4  1   1   TRUE     0         0   0      1     0              0    0    0     0     0
5  1   1  FALSE     0         0   0      0     0              0    0    0     0     1
6  1   2  FALSE    -1         0   0      0     0              0    0    0     0     0

with it's structure as

'data.frame':   18720 obs. of  13 variables:
 $ ID            : int  1 1 1 1 1 1 1 1 1 1 ...
 $ Set           : num  1 1 1 1 1 2 2 2 2 2 ...
 $ Choice        : logi  FALSE FALSE FALSE TRUE FALSE FALSE ...
 $ Apple         : num  1 0 0 0 0 -1 0 0 0 0 ...
 $ Microsoft     : num  0 1 0 0 0 0 -1 0 0 0 ...
 $ IBM           : num  0 0 1 0 0 0 0 -1 0 0 ...
 $ Google        : num  0 0 0 1 0 0 0 0 -1 0 ...
 $ Intel         : num  0 0 0 0 0 0 0 0 0 0 ...
 $ HewlettPackard: num  0 0 0 0 0 0 0 0 0 0 ...
 $ Sony          : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Dell          : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Yahoo         : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Nokia         : num  0 0 0 0 1 0 0 0 0 -1 ...

upon running mlogit with the following code:

dt = mlogit.data(longData, choice = 'Choice', shape = 'long', id.var = 'ID')

I am getting the following error

Error in dfidx::dfidx(data = data, dfa$idx, drop.index = dfa$drop.index,  : 
  impossible to coerce the choice variable to a logical

But I am unable to figure out why I am getting this error, despite the fact that my Choice column is of logical type. To reproduce the data.frame you can take help of this file.

  • Watch your syntax: it's likely `Choice` should not be quoted. (Since `mlogit.data` isn't a part of base `R`, the answer ultimately depends on the code for that function.) – whuber Sep 03 '20 at 14:56
  • @whuber thanks for your reply, but choice need to be in quote- without it I get error ```Error in eval(dfa[[i]], parent.frame()) : object 'Choice' not found```. So how to approach this, I am going banana on this problem. You can see the [same question](https://stackoverflow.com/questions/63719959/error-when-using-mlogit-impossible-to-coerce-the-choice-variable-to-a-logical?noredirect=1#comment112679630_63719959) that I posted on SO – Ankit Agrawal Sep 03 '20 at 15:16
  • Does this field have any missing values in the data frame? – whuber Sep 03 '20 at 15:25
  • No they don't have any missing values – Ankit Agrawal Sep 03 '20 at 16:14

0 Answers0