title: R: ifelse function returns vector position instead of value (string)
body: <p>I have a very strange problem concerning the ifelse function: it does not return a factor (as I want) but something like the position of the factor. </p>
  
  <p>The dataset I use can be downloaded <a href="https://www.dropbox.com/s/aq04whf1yju0m2n/aDDs.csv" rel="nofollow noreferrer">here</a>.</p>
  
  <h3>What I want</h3>
  
  <p>..is to make a new column in df that contains the name of the country IF that country belongs to the top 12 most frequent countries (in the column "answer"). Else it should contain "Other"</p>
  
  <h3>What I did</h3>
  
  <p>... is </p>
  
  <ul>
  <li>Create a list with the most frequent country names using as.data.frame(summary.. etc)  ##this works</li>
  <li>The TRUE part of the function matches df$col value with this list using %in% ##this works also</li>
  <li>Return value if TRUE should be the factor (a country name) in that </li>
  </ul>
  
  <h3>However</h3>
  
  <p>... R returns something really strange: it returns the position of the factor level (between 1 and 181) for the top 10 countries, and "Other" for the others (which is ok). It is this line that returns the wrong value:</p>
  
  <pre><code>        aDDs$answer, ## then it should be named as aDDs$answer **THIS IS THE PROBLEM**
  </code></pre>
  
  <h3>The code I used:</h3>
  
  <pre><code>## create a list with most frequent country names
  temp &lt;- row.names(as.data.frame(summary(aDDs$answer, max=12))) # create a df or something else with the summary output.
  colnames(temp)[1]="freq"
  "India" %in% temp #check if it works (yes)
  
  ## create new column that filters top results
  aDDs$top &lt;- ifelse(
          aDDs$answer %in% temp, ## condition: match aDDs$answer with row.names in summary df 
          aDDs$answer, ## then it should be named as aDDs$answer **THIS IS THE PROBLEM**
          "Other" ## else it should be named "Other"
        )
  
  View(aDDs)
  </code></pre>
  
  <p>PS. This is a follow-up question to <a href="https://stackoverflow.com/questions/21596906/ggplot-how-to-limit-output-in-bar-plot-so-only-most-frequent-occurrences-are-sh">this one</a>, because it is somewhat different, and may need a separate question.</p>