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.
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.
attach() – Attach a set of R Objects to Search Path
buildingSurvey <- data.frame(name=c("bldg1", "bldg2", "bldg3",
"bldg4", "bldg5", "bldg6"),
floors=c(5, 10, 10, 11, 8, 12),
attach() function to make the variables in
"buildingSurvey" independently searchable. Then, use “
summary()” to create a summary of the “
Using the “
summary()” function, find the median “
efficiency” value of “
buildingSurvey“, using objects in the R environment search path.
Once attached, in order to change the dataframe variable, use the assignment operator “
<<-“. For example:
variable1 <<- log(variable1)
<<-” to divide the “
efficiency” category by
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, “
transform()” function performs a transformation on a dataframe object.
transform() to replace the “
efficiency” column’s values with the starting values divided by
First, re-attach the dataframe, “
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 “
transform() to round the “
efficiencyLog” variable of “
efficiencyL” to one decimal place.
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
within() function to set
efficiency to “
85“. This will also create a copy of “
buildingSurvey“. Setting a new dataframe isn’t required for this exercise.
For the final exercise, restore the R environment search path.
- Become a Top R Programmer Fast with our Individual Coaching Program
- Explore all our (>4000) R exercises
- Find an R course using our R Course Finder directory
- Subscribe to receive weekly updates and bonus sets by email
- Share with your friends and colleagues using the buttons below