(R Example for Citizen Data Scientist & Business Analyst)
This code uses a dataset file with population estimates by the US Census Bureau (more info).
tbl <- read.table(file.choose(),header=TRUE,sep=',') population <- tbl[c("NAME","POPESTIMATE2009","NPOPCHG_2009")] smallest.state.pop <- min(population$POPESTIMATE2009) print(population[population$POPESTIMATE2009==smallest.state.pop,])
NAME POPESTIMATE2009 NPOPCHG_2009 56 Wyoming 544270 11289
This piece of code extracts the data about the smallest state from the data frame.
Picking specific columns out of a data frame
The second line limits the rows to the state name, the population estimate for 2009 and the total population change for 2009.
Let’s use the head function to look at what we get:
head(population)
NAME POPESTIMATE2009 NPOPCHG_2009 1 United States 307006550 2631704 2 Northeast 55283679 223483 3 Midwest 66836911 241314 4 South 113317879 1296857 5 West 71568081 870050 6 Alabama 4708708 31244
Finding the lowest value in a list
First the POPESTIMATE2009 column is selected:
population$POPESTIMATE2009
[1] 307006550 55283679 66836911 113317879 71568081 4708708 698473 [8] 6595778 2889450 36961664 5024748 3518288 885122 599657 [...] [50] 2784572 621760 7882590 6664195 1819777 5654774 544270 [57] 3967288
Then the min function is used to find the minimum:
min(population$POPESTIMATE2009)
[1] 544270
Selecting the row with the lowest population value
You use something like a WHERE clause in data frame indices:
data.frame[condition]
This condition works because it creates an array of booleans depending on whether the field value is a match:
population$POPESTIMATE2009==smallest.state.pop
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [49] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
In this case only the second to last row should be selected. We use a comma after the row index because we want all the columns:
population[population$POPESTIMATE2009==smallest.state.pop,]
NAME POPESTIMATE2009 NPOPCHG_2009 56 Wyoming 544270 11289
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.