-1

I've created a vector for age,

age <- c(18, 21, 22, 17, 19, 19, 20)

and also a vector for names.

name <- c("Emily", "John", "Michael", "Alex", "Olivia", "Sophia", "Noah")

The names correspond to the ages. I am supposed to find the people who are older than 18. I know how to return for who has the youngest age through

name[which.min(age)]

which returns Alex, but I'm stuck on how to get everyone who is older than 18 (which should be everyone except for Alex). Thanks!

2 Answers2

0

You can try

name[age > 18]

or

subset(name,age>18)
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
0

You can combine the vectors in a dataframe and subset the dataframe on your condition:

df <- data.frame(age, name)
df$name[df$age >= 18]
[1] Emily   John    Michael Olivia  Sophia  Noah   
Levels: Alex Emily John Michael Noah Olivia Sophia
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34