title: How do I calculate two separate means for a vector with both characters and numbers?
body: <p>New to R.. I created a BMI variable and pasted it with the sex variable so I can see which BMI is labeled  M (male) or F (female). How do I find the mean of group M and group F separately? </p>
  
  <p>I've tried using the substr and gsub functions to get rid of the characters but am not sure that's the solution because then I have no way of knowing whether the BMI value should be calculated as M or F. </p>
  
  <p>edit: </p>
  
  <p>I want to calculate the means for M and F separately to learn how to do simple subgroup analyses. I've been trying to learn R independently, and this particular BMI problem is from an old problem set. </p>
  
  <p>edit: </p>
  
  <p>I see why pasting doesn't work here. I was treating the <code>paste</code> function like <code>proc format</code> in SAS. </p>
  
  <pre><code>set.seed(123)
  sex &lt;- sample(x = c("M", "F"), size = 100, replace = TRUE)
  height.cm &lt;- rnorm(n = 100, mean = ifelse(sex == "M", 175, 163), sd = 3)
  weight.kg &lt;- -110 + height.cm * 1.1 + rnorm(n = 100, sd = 7)
  waist.in &lt;- -20 + ifelse(sex == "M", 35, 33) + 0.5 * weight.kg + rnorm(n = 100, sd = 2.5)
  
  bmi &lt;- weight.kg / (height.cm / 100) ^ 2
  
  bmi_sex &lt;- paste(sex, bmi, sep = "")
  
  </code></pre>