attribute() function in R Language is used to get all the attributes of data. This function is also used to set new attributes to data.
Syntax: attributes(x)
Parameters:
x: object whose attributes to be accessed.
Example 1: Implementing attributes() function
# R program to illustrate # attributes function # Load info set that you want to work on data(info) # Print first 6 rows of info set data head(info) # Apply attributes function attributes(info) |
Output:
$`names` [1] Ram [2] Sham $class [1] data.farme $roll no. [1] [2]
Here in the above code, we have applied the attributes()
function so we have shown all the data which is present in the info data frame.
So by applying info all the attributes in the datasets will be shown.
Example 2: Assigning new value to the attributes in R language
# R program to illustrate # adding new attributes # Load info set that you want to work on data(info) # Set different column names # retain dataframe class attributes_list < - list (names = c( "Jhon " , "Lee" ), class = "data.frame" , roll no = c( "71" , "72" ) # New attributes from list added to info database >attributes(info) < - attributes_list attributes(info) |
Output:
[1] jhon [2] lee $class [1] data.frame $roll no. [1] 71 [2] 72
Here in the above code, we have added the new attributes to the list info from Example 1, and then printed all the attributes including the new one.
attr() Function
attr()
will return specfic data, but this functions needs a precised information about the data.
Syntax:
attr(x = data, which = “attribute_name”)Parameters:
x: object whose attributes to be accessed.
which: string specifying the attribute to be accessed.
Example: Implementing attr() function
# R program to illustrate # attr() function # Load info set that you want to work on data(info) # Apply attr() function attr(x = info, which = "names" ) |
Output:
[1] Ram [2] Sham
Here in the above code, we have specified a particular parameter name, so only the values in names will be returned.
attr()
will return specific data, but attr() functions need a precise information about the data.