Accessing Dataframe Objects Exercises

[This article was first published on R-exercises, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

access dataframe
The attach() function alters the R environment search path by making dataframe variables into global variables. If incorrectly scripted, the attach() function might create symantic errors. To prevent this possibility, detach() is needed to reset the dataframe objects in the search path.

The transform() function allows for transformation of dataframe objects. The within() function creates a new dataframe, when modifying dataframe variables.

Answers to the exercises are available here.

Exercise 1
attach() – Attach a set of R Objects to Search Path

Required Dataframe: buildingSurvey <- data.frame(name=c("bldg1", "bldg2", "bldg3", "bldg4", "bldg5", "bldg6"), survey=c(1,1,1,2,2,2), location=c(1,2,3,2,3,1), floors=c(5, 10, 10, 11, 8, 12), efficiency=c(51,64,70,71,80,58))

Use the attach() function to make the variables in "buildingSurvey" independently searchable. Then, use “summary(location)” to create a summary of the “floors” variable.

Exercise 2
Using the “summary()” function, find the median “efficiency” value of “buildingSurvey“, using objects in the R environment search path.

Exercise 3
Once attached, in order to change the dataframe variable, use the assignment operator “<<-“. For example: variable1 <<- log(variable1)

Use “<<-” to divide the “efficiency” category by 100.

Exercise 4
detach() – Detach Objects from the Search Path

After detaching, modified attach() dataframes are restored to their pre-attach() values. and the R environment search path is restored. detach() is needed to prevent symantec errors in programming.

Therefore, use the detach() function to restore the search paths of the dataframe, “buildingSurvey“.

Exercise 5
The “transform()” function performs a transformation on a dataframe object.

Use transform() to replace the “efficiency” column’s values with the starting values divided by 100.

Exercise 6
First, re-attach the dataframe, “buildingSurvey“.

Then, use transform() to evaluate the log of the “efficiency” variable. Set the result to the dataframe, “efficiencyL“. The column names of the dataframe “efficiencyL” should be “X_data“, and “efficiencyLog“.

Exercise 7
Next, use transform() to round the “efficiencyLog” variable of “efficiencyL” to one decimal place.

Exercise 8
The within() function creates a modified copy of a dataframe.

For this exercise, use within() to append the “buildingSurvey” dataframe with a variable called, “efficiency10“. The new variable contains “efficiency” multiplied by 10.

Exercise 9
Use the within() function to set efficiency[4] to “85“. This will also create a copy of “buildingSurvey“. Setting a new dataframe isn’t required for this exercise.

Exercise 10
For the final exercise, restore the R environment search path.

To leave a comment for the author, please follow the link and comment on their blog: R-exercises.

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.

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)