title: I have an error in missing values are not allowed in subscripted assignments of data frames
body: <p>I am new to R and I am constructing R codes for my personal project/exercise. The data I am using is about a survey on ethnic identity of people from Hongkong. I used 2019 data from <a href="http://data.hkupop.hku.hk/v3/hkupop/ethnic_identity/ch.html" rel="nofollow noreferrer">http://data.hkupop.hku.hk/v3/hkupop/ethnic_identity/ch.html</a>. </p>
  
  <p>After removing NA values and reducing the columns to that of my necessity, 
  I noticed that the data is highly imbalanced so I tried to use under-sampling, ROSE and SMOTE. (the number had greatly reduced from 1015 observations to 573)</p>
  
  <h1>I removed the following column # from the set</h1>
  
  <pre><code>df_f &lt;- df[,-c(1,2,5,6,8,9,11,12,14,15,17,18,20,21,25,26,27,29,32,33,34,35,37)]
  </code></pre>
  
  <p>However, this is not a binary data, thus I had to force the factors in eth_id to combine into 0 = 1&amp;3 (Hong Konger and Hong Kong Chinese) and 1 = 2&amp;4 (Chinese and Chinese Hong Kong citizen) </p>
  
  <h1>How I combined the factors</h1>
  
  <pre><code>df_p$eth_id &lt;- recode(df_p$eth_id, "c('1', '3')='1+3';c('2', '4') = '2+4'")
  
  library(plyr)
  
  revalue(df_p$eth_id, c('1+3' = 0)) -&gt; df_p$eth_id
  revalue(df_p$eth_id, c('2+4' = 1)) -&gt; df_p$eth_id
  </code></pre>
  
  <ul>
  <li>0 = Hong Kong Citizen + Hong Kong Chinese Citizen </li>
  <li>1 = Chinese Citizen + Chinese Hong Kong Citizen </li>
  </ul>
  
  <h1>How I renamed the columns</h1>
  
  <pre><code>df_f &lt;- df_f %&gt;%  
                rename(
                          eth_id = Q001,
                          HongKonger = Q002A, 
                          Chinese = Q003A, 
                          PRC = Q004A,
                          CH_race = Q005A, 
                          Asian = Q006A, 
                          global = Q007A,
                          class1 = mid, 
                          housing1 = type, 
                          housing2 =  housingv2, 
                          pi = inclin 
                        )
  </code></pre>
  
  <h1>HOW I PROCESSED MY NAs and unnecessary outliers</h1>
  
  <p>For the columns [,2:7], I changed their values to 0 for NAs, For example, <code>df_f$HongKonger &lt;- ifelse(is.na(df_f$HongKonger),0,df_f$HongKonger)</code> so on and so forth. </p>
  
  <p>And for the others, I removed the NAs like this: </p>
  
  <pre><code>df_p &lt;- na.omit(df_p, cols= c("eth_id","sex","agegp","edugp","occgp","class","class2","housing1","housing2","pi"), invert=FALSE) 
  </code></pre>
  
  <p>At this point of my data set, I was left with 14 columns and I renamed them (please refer to above). I uploaded the final structure of my data below which I used for ROSE and SMOTE :-) </p>
  
  <p>Furthermore, I also removed rows that were outliers like:</p>
  
  <h1>Remove an unidentifiable ethnic_identity (8881 or level = 5)</h1>
  
  <pre><code>df_f &lt;- df_f[!df_f$Q001 == "8881",] table(df_f$Q001) 
  df_f &lt;- df_f[!df_f$eth_id == "Don't know / hard to say",] 
  </code></pre>
  
  <ul>
  <li>these codes must be carefully written, if you run it before the renaming please use eth_id in place of Q001 and vice-versa. </li>
  </ul>
  
  <p>Now, I kept on getting this error when I run ROSE: 
  Error in <code>[&lt;-.data.frame</code>(<code>*tmp*</code>, , indY, value = c(1L, 1L, 1L, 1L, 1L, : missing values are not allowed in subscripted assignments of data frames. </p>
  
  <p>This is very misleading because I made sure to remove NA values completely (because all the questions related to this were related to NA issue, which is not applicable to mine) and I even changed all my factor values to numerical. 
  (Because I thought that the program is not understanding? the factor values.) </p>
  
  <p>I am also getting this error message for SMOTE: Error in names(dn) &lt;- dnn : attempt to set an attribute on NULL. This mak</p>
  
  <pre><code>es me even more confused to the level that I am doubting the data itself being not applicable to machine learning. 
  
  Here is the final structure of my data for your reference: 
  'data.frame':   573 obs. of  14 variables:
   $ eth_id    : Factor w/ 2 levels "0","1": 2 2 1 2 1 1 1 1 1 1 ...
   $ HongKonger: num  9 0 0 0 0 2 0 2 0 8 ...
   $ Chinese   : num  9 9 1 3 7 0 7 9 0 0 ...
   $ PRC       : num  8 9 1 3 7 3 1 0 1 0 ...
   $ CH_race   : num  12 10 0 3 7 3 0 7 3 4 ...
   $ Asian     : num  0 7 6 0 0 2 2 0 0 6 ...
   $ global    : num  0 0 0 0 0 3 7 0 10 0 ...
   $ sex       : num  1 2 2 1 2 1 1 2 1 2 ...
   $ agegp     : num  6 5 2 2 6 5 2 4 6 1 ...
   $ edugp     : num  2 3 2 3 1 2 2 2 3 3 ...
   $ class1    : num  3 3 3 5 3 3 4 4 4 3 ...
   $ housing1  : num  1 1 2 2 1 2 1 2 1 1 ...
   $ housing2  : num  3 3 1 4 3 1 2 1 3 3 ...
   $ pi        : num  3 2 1 2 1 1 1 4 1 1 ...
   - attr(*, "na.action")= 'omit' Named int  14 24 46 52 58 67 77 84 94 129 ...
    ..- attr(*, "names")= chr  "25" "44" "82" "90" ...
  
  #How I divided the data into train and test set 
  
      set.seed(123)
      index &lt;- createDataPartition(df_p$eth_id, p = 0.7, list = FALSE)
      train_data &lt;- df_p[index, ]
          test_data  &lt;- df_p[-index, ]
  
      head(test_data)
      str(train_data)
  
      #How I used ROSE for under-sampling  
  
  library(ROSE)
  ovun.sample(formula = train_data$eth_id ~ ., data = train_data, method="under", N = 250,seed = 123)$data
  </code></pre>
  
  <h1>How I used ROSE for "both"</h1>
  
  <pre><code>ovun.sample(formula = train_data$eth_id ~ . , data = train_data, method="both",
              na.action=options("na.omit")$na.action,p=0.5,seed = 123)$data
  </code></pre>
  
  <h1>How I used SMOTE</h1>
  
  <pre><code>SMOTE(form = train_data$eth_id ~., data = train_data, perc.over = 100, k = 5, perc.under = 200)
  </code></pre>
  
  <p>I am keep on getting : 
  1) for ROSE: Error in <code>[&lt;-.data.frame</code>(<code>*tmp*</code>, , indY, value = c(1L, 1L, 1L, 1L, 1L, : missing values are not allowed in subscripted assignments of data frames </p>
  
  <p>2) for SMOTE: Error in names(dn) &lt;- dnn : attempt to set an attribute on NULL</p>
  
  <ul>
  <li>I am also confused changing all the factors into numeric value would make it still valid.</li>
  </ul>
  
  <p>Thank you and thank you for sharing your knowledge ahead.</p>