Getting attributes of Objects in R Language – attributes() and attr() Function

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)                                       

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)                  

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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")                      

chevron_right


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.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.