Dim and Dimnames in R

In data science, it is often necessary to get information about a data set to see what is in it. The names of the columns and rows say much about the contents of a data set. Furthermore, it allows the program to make use of this information outside of the data set.

Dimnames in R

Retrieving the names of the rows and columns from a data set can be tricky in most programming languages. However, dimnames in R does it with a single function. The dimnames() function is in the form of dimnames(data set), and it returns the name of the rows in columns.

# dimnames in r 
> dimnames(mtcars)
 [[1]]
  [1] "Mazda RX4"           "Mazda RX4 Wag"       "Datsun 710"         
  [4] "Hornet 4 Drive"      "Hornet Sportabout"   "Valiant"            
  [7] "Duster 360"          "Merc 240D"           "Merc 230"           
 [10] "Merc 280"            "Merc 280C"           "Merc 450SE"         
 [13] "Merc 450SL"          "Merc 450SLC"         "Cadillac Fleetwood" 
 [16] "Lincoln Continental" "Chrysler Imperial"   "Fiat 128"           
 [19] "Honda Civic"         "Toyota Corolla"      "Toyota Corona"      
 [22] "Dodge Challenger"    "AMC Javelin"         "Camaro Z28"         
 [25] "Pontiac Firebird"    "Fiat X1-9"           "Porsche 914-2"      
 [28] "Lotus Europa"        "Ford Pantera L"      "Ferrari Dino"       
 [31] "Maserati Bora"       "Volvo 142E"     
[[2]]
  [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear" "carb"   

Section 1 contains a list of the rows in the mtcars data set. It has a list of thirty-two cars. Section 2 has the eleven columns showing the specifications for these cars.

Dim in R

One simple way of getting the number of rows and columns in a data set is by dim in R. The dim() function has the form of dim(data set), and it returns the number of rows and columns in that data set.

# dim in r 
> dim(mtcars)
 [1] 32 11

The results here are that the mtcars data set has thirty-two rows, each representing a car. Furthermore, it has eleven columns, each representing a specification of these cars.

As can be seen by the example from the mtcars data set, these two functions provide a lot of information about the data set. Having the row and column information tell us a lot about the content of the data set. For example, in this case, the mtcars data set is a list of cars and their specifications. Having two functions to retrieve column and row names as well as numbers of each saves a lot of programming to get this information. It is yet another reason R he’s such an excellent data science tool.