Select and Query Exercise
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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
a.select the first column of the df using column name
b.select 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
d.select the second column of the df using index method
e.select 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
e.select 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
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.