简体   繁体   English

对 r 中的值进行排序以制作堆叠箱线图

[英]Sorting values in r to make stacked boxplots

I have a dataframe containing the ages and sex of people with heart disease.我有一个 dataframe 包含心脏病患者的年龄和性别。 From this, I would like to take 2 vectors, each containing all the ages for one of the sexes.由此,我想取 2 个向量,每个向量都包含其中一种性别的所有年龄。 However, I can't figure out how to do this.但是,我无法弄清楚如何做到这一点。 Any help is appreciated: The data looks like this:任何帮助表示赞赏:数据如下所示:

          age sex 
   1      63   1 
   2      37   1 
   3      41   0 
   4      56   1 
   5      51   0

I would want my two vectors to be (63, 37, 56) and (41, 51).我希望我的两个向量是 (63, 37, 56) 和 (41, 51)。

I've tried doing males <- data$sex==1 females <- data$sex==1 but this just gives me 2 vectors full of true or false and not the lines of data containing the sex and age.我试过做males <- data$sex==1 females <- data$sex==1但这只是给了我 2 个充满真假的向量,而不是包含性别和年龄的数据行。 I am unsure what other options I have to try or how to word a google search for them.我不确定我必须尝试哪些其他选项或如何用谷歌搜索来搜索它们。

It's not very clear what you're after since the title of the question doesn't seem to relate with the question itself.由于问题的标题似乎与问题本身无关,因此您的目标不是很清楚。 Solutions for both:两者的解决方案:

df <- data.frame(
  age = c(63, 37, 41, 56, 51)
  ,sex = c(1, 1, 0, 1, 0)
)

df[df$sex == 1,] # gives you data frame for males, assuming sex == 1 denotes males

You don't have to sort data for boxplot.您不必为箱线图排序数据。 You could simply do this:你可以简单地这样做:

boxplot(age ~ sex, data = df)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM