I have a dataframe containing the ages and sex of people with heart disease. From this, I would like to take 2 vectors, each containing all the ages for one of the sexes. 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).
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. 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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.