Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert data.frame columns from factors to characters
    text
    copied!<p>I have a data frame. Let's call him <code>bob</code>:</p> <pre><code>&gt; head(bob) phenotype exclusion GSM399350 3- 4- 8- 25- 44+ 11b- 11c- 19- NK1.1- Gr1- TER119- GSM399351 3- 4- 8- 25- 44+ 11b- 11c- 19- NK1.1- Gr1- TER119- GSM399352 3- 4- 8- 25- 44+ 11b- 11c- 19- NK1.1- Gr1- TER119- GSM399353 3- 4- 8- 25+ 44+ 11b- 11c- 19- NK1.1- Gr1- TER119- GSM399354 3- 4- 8- 25+ 44+ 11b- 11c- 19- NK1.1- Gr1- TER119- GSM399355 3- 4- 8- 25+ 44+ 11b- 11c- 19- NK1.1- Gr1- TER119- </code></pre> <p>I'd like to concatenate the rows of this data frame (this will be another question). But look:</p> <pre><code>&gt; class(bob$phenotype) [1] "factor" </code></pre> <p><code>Bob</code>'s columns are factors. So, for example:</p> <pre><code>&gt; as.character(head(bob)) [1] "c(3, 3, 3, 6, 6, 6)" "c(3, 3, 3, 3, 3, 3)" [3] "c(29, 29, 29, 30, 30, 30)" </code></pre> <p>I don't begin to understand this, but I guess these are indices into the levels of the factors of the columns (of the court of king caractacus) of <code>bob</code>? Not what I need.</p> <p>Strangely I can go through the columns of <code>bob</code> by hand, and do</p> <pre><code>bob$phenotype &lt;- as.character(bob$phenotype) </code></pre> <p>which works fine. And, after some typing, I can get a data.frame whose columns are characters rather than factors. So my question is: how can I do this automatically? How do I convert a data.frame with factor columns into a data.frame with character columns without having to manually go through each column? </p> <p>Bonus question: why does the manual approach work?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload