Accessing variables of a data frame in R Programming - attach() and detach() function Last Updated : 24 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will see how to Access variables of a data frame in R Programming Language. R - Accessing variables of a data frameMethod 1: Using attach() function in Rattach() function in R Language is used to access the variables present in the data framework without calling the data frame. Syntax: attach(data, pos) Parameters: data: data framepos: position of databaseExample: Accessing variables of a data frame in R using attach() function R # R program to illustrate # attach function # Create example data data <- data.frame(x1 = c(1, 2, 3, 4, 5), x2 = c(6, 7, 8, 9, 0), x3 = c(1, 2, 5, 4, 5)) # Try to print x1 # Error: object 'x1' not found # attach data attach(data, pos = x1) Output: 1 2 3 4 5Here in the above code, we have created a data framework and assigned a value to it, when we tried to return value an error occurred. Then we use the attach function and returned value of x1. Method 2: Using detach() Functiondetach() function is used to remove the attachment in data framework that was made by attach() function. Syntax: detach(data, unload) Parameters: data: data frameunload: boolean valueExample: Accessing variables of a data frame in R using detach() Function R # R program to illustrate # detach function # Install dplyr package install.packages("dplyr") # attach dplyr library("dplyr") # Apply as.tbl function of dplyr package data_tbl <- as.tbl(data) detach("package:dplyr", unload = FALSE) # Apply as.tbl after detaching dplyr package data_tbl <- as.tbl(data) Output: Error in as.tbl(data) : could not find function "as.tbl" Here in the above code, we have installed a dplyr package and used its function as.tbl. Then we detach the package and try to use the function again, and an error occurred. detach functions are used to unpack the libraries which were added to the library. Comment More infoAdvertise with us Next Article Rename Columns of a Data Frame in R Programming - rename() Function A akhilsharma870 Follow Improve Article Tags : R Language Similar Reads Create Subsets of a Data frame in R Programming - subset() Function subset() function in R Programming Language is used to create subsets of a Data frame. This can also be used to drop columns from a data frame.Syntax: subset(df, expr)Parameters: df: Data frame usedexpr: Condition for subsetCreate Subsets of Data Frames in R Programming LanguageHere we will make sub 3 min read Convert an Object to Data Frame in R Programming - as.data.frame() Function as.data.frame() function in R Programming Language is used to convert an object to data frame. These objects can be Vectors, Lists, Matrices, and Factors. Syntax: as.data.frame(object) Parameters:Â object: Vector, Matrix, factor, or data frameR - as.data.frame() Function ExampleExample 1: Basic exam 2 min read Rename Columns of a Data Frame in R Programming - rename() Function The rename() function in R Programming Language is used to rename the column names of a data frame, based on the older names.Syntax: rename(x, names) Parameters:x: Data frame names: Old name and new name 1. Rename a Data Frame using rename function in RWe are using the plyr package to rename the col 2 min read Assigning values to variables in R programming - assign() Function In R programming, assign() method is used to assign the value to a variable in an environment. Syntax : assign(variable, value) Return : Return the variable having value assigned. Example 1: Python3 # Using assign() method assign("gfg", 10) print(gfg) Output: [1] 10 Example 2: Python3 # Us 1 min read The Factor Issue in a DataFrame in R Programming DataFrames are generic data objects of R which are used to store the tabular data. Data frames are considered to be the most popular data objects in R programming because it is more comfortable to analyze the data in the tabular form. Data frames can also be taught as matrices where each column of a 4 min read Evaluating an Expression in R Programming - with() and within() Function with() function in R programming evaluates the expression in an environment constructed locally by the data and does not create a copy of the data. Syntax: with(data, expr) Parameters: data represents dataset to be used expr represents formula or expression to be evaluated Example 1: Python3 # Creat 2 min read Like