# using the "Order" function to determine the US state with smallest population size, from a data set

I'm trying to order a data set using the following:
library(dslabs)
data(murders)
pop<- murders\$population
pop_min_index <- order(pop)
min(pop_min_index)

When I use this, R presents "pop_min_index" as all the values from the data set, when I just want the first value, i.e. the answer should be "51", where as my result is coming out as "51, 9, 46, 35, 2, 42", etc.

I've tried using:
min(pop_min_index)

but this doesn't seem to work either

Does anyone know what is wrong my code?
Thanks

in general if you have a vector and want only the first entry use

``````head(order(pop),n=1)
``````

will give 51

Thanks,
still seems to be keeping pop_min_index as the full list:

basically I am completing a practice exercise in R, and the question is as follows:

well, if you need to assign the result to a specific name then thats done with the `<-` operator. so in this case

``pop_min_index <- head(order(pop),n=1)``

THANK YOU

I hadn't even heard of "head", is this the only way to do this question, as we haven't been taught this yet, so just wondering if there is some content missing from the instructions i've been given?

you may have been taught how to arbitrarily index into a vector with square brackets.
In this case that would be

``pop_min_index <- order(pop)``

That was exactly my issue. I kept inputting that as:
library(dslabs)
data(murders)
pop<- murders\$population
pop_min_index <- order(pop)
pop_min_index

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.