Select and Query Exercise

December 6, 2016

(This article was first published on R-exercises, and kindly contributed to R-bloggers)


In this exercise we cover the basics on selecting and extracting data using queries. We add a few other materials into it. This should prepare you for the next exercise: Basic Decision Tree. The purpose of this is to give you the 20 percent of the tools to get 80 percent of work done. You will be off and running plotting simple decision trees. It will be helpful to read up on str(),colnames() and data.frame() command.

Answers to the exercises are available here.

If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.

Exercise 1
create vectors. Hint: use c outside brackets to indicate multiple items
a. Create a numeric vector that includes the numbers 4, 5,6,8 and 3 (exact order). Store this in the variable a
b. Create a character vector that includes the characters "apple","chair","jetplane","salmon","island". Store this in the variable b
c. Create a boolean vector TRUE,TRUE,FALSE,TRUE,FALSE. Note that boolean is sensitive to letter case. Store this in c

Exercise 2
create a dataframe using the variables a,b and c. Store this in df

Exercise 3
use the str() command to see the dataframe

Exercise 4
print the column names of df

Exercise 5
change the column names of df to “id”,”wish”,”real”. View the dataframe again using the str() command.

Exercise 6
quick selecting exercise the first column of the df using column name the row where id==3. Use this logic to select the row.
c.Now use the index method to select the first row in df the second column of the df using index method the second item in the third row of df

Exercise 7
attach/load the iris dataset and use the str() command to see the dataset

Exercise 8
change the column names of iris to "sl","sw","sl","pw","s"

Exercise 9
select exercise using the iris dataset
a. select the rows where ‘sw’ is 3
b. select the first element of the second cloumn
c. select the first 6 rows of the dataset
d. select the last 6 rows of the dataset every column except the s column

Exercise 10
select all the rows where sl is greater than 5 and store all the sample in iris_2 making sure to exclude the s column

To leave a comment for the author, please follow the link and comment on their blog: R-exercises. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)