DAY 3 (PART-1): Attributes of R objects

SaiGayatri Vadali
3 min readDec 21, 2017

--

This article is the third one in the series - Getting started with data science in 30 days using R programming. This series is an attempt of mine to create a quick reference notes useful for all apart from helping newbies to get started. I highly recommend to go through the previous two articles if you are new to R.

Attributes or Metadata of R objects

Attributes constitute meta data of R objects. They give the basic understanding of the objects like vectors, data frames. For example, if you have taken a data set and stored it in a data frame object, you might first want to know about the length , dimensions and columns of it. Attributes of objects help us getting this kind of information. There are mainly four attributes

1. Names and dimension names

2. Dimensions

3. Class or data type

4. Length

1. Names and dimension names:

Names of R objects help in writing readable code. Names are generally user defined strings given to elements of objects. Here are the examples of a vector, a list and their names

> friends <- 1:5
> names(friends)<-c("Bob","mars","Jack","Lica")
> names(friends)
[1] "Bob" "mars" "Jack" "Lica" NA

Find out what ‘NA’ is. It is the notation given to missing value. As I have not given the fifth name, R considered it as NA. More about this in coming articles.

Matrices have dimension names namely column names and row names. Here is the code snippet showing the same

> m <- matrix(1:4,2,2)
> dimnames(m)<-list(c("a","b"),c("c","d"))
> m
c d
a 1 3
b 2 4

Column names and row names of matrices and data frames:

> rownames(m)
[1] "a" "b"
> colnames(m)
[1] "c" "d"

Data frames have slightly different syntax for column names and row names. Let’s take the inbuilt data frame ‘mtcars’ to know about these. You can type mtcars in the console to view it or know more about it here.

Column names:

> names(mtcars)
[1] "mpg" "cyl" "disp" "hp" "drat" "wt"
[7] "qsec" "vs" "am" "gear" "carb"

Row names:

>rownames(mtcars)
[1] "Mazda RX4" "Mazda RX4 Wag"
[3] "Datsun 710" "Hornet 4 Drive"
[5] "Hornet Sportabout" "Valiant"
[7] "Duster 360" "Merc 240D"
[9] "Merc 230" "Merc 280"
[11] "Merc 280C" "Merc 450SE"
[13] "Merc 450SL" "Merc 450SLC"
[15] "Cadillac Fleetwood" "Lincoln Continental"
[17] "Chrysler Imperial" "Fiat 128"
[19] "Honda Civic" "Toyota Corolla"
[21] "Toyota Corona" "Dodge Challenger"
[23] "AMC Javelin" "Camaro Z28"
[25] "Pontiac Firebird" "Fiat X1-9"
[27] "Porsche 914-2" "Lotus Europa"
[29] "Ford Pantera L" "Ferrari Dino"
[31] "Maserati Bora" "Volvo 142E"

Dimensions:

Dimensions of any R object can be obtained with dim() function. It gives a vector with number of columns and number of rows as output.

> dim(mtcars)
[1] 32 11

Class:

class() method enables us to know about the data type of the object. When at times, we are really clueless as to what to do with an R object, this function comes handy giving us some idea about the object and possible operations which we can do.

> class(friends)
[1] "integer"
> friends
Bob mars Jack Lica <NA>
1 2 3 4 5

Length:

Length() method gives the information about length of the objects. Following examples elucidate it in a better way

> length(friends) # an example of vector
[1] 5
> length(mtcars) # example elucidating a data frame
[1] 11
> length(m) # example of a matrix
[1] 4

We can see that in a vector, length() function gives the length of vector, in matrix it gives total number of elements in the matrix and in a data frame, it gives the length of number of columns.

This takes us to the end of first part of today. There is a second part too which explains about data frames. Don’t go away without reading it!!

How did feel about this series till now ? What else do you want me to add?? Your response keeps motivating me to provide the best content!!! So keep reading and commenting. Happy Learning !!

--

--

SaiGayatri Vadali

An inquisitive Machine Learning Engineer, yoga trainer, fitness freak and a passionate writer!