Dr. Mark Gardener 


Home  
Providing training for:

Tips and Tricks  for R and ExcelOn this page you can find tips, tricks and hints for using both R and Excel. At the end of each tip there are links forwards and backwards as appropriate.There is also an index of R tips and an index of Excel tips. For most analytical purposes the combination of Excel and R is unbeatable! Excel is great as a data management tool and for preparing data for analysis. You can also use it to get an overview of your data or to make simple (and not so simple) graphs. R is an analytical "swiss army knife" and can carry out a mindboggling array of analytical routines as well as producing great graphics. Tips & Tricks for R  Tips & Tricks for Excel  An Introduction to R  MonogRaphs  Writer's Bloc 

Row/column names for matrix, array and data.frame 
Row & column names using dimnames()The dimnames() command can set or query the row and column names of a matrix. Unlike rownames() or colnames() the dimnames() command operates on both rows and columns at once. If you use it to set the names you need to specify the names for the rows and columns in that order) in a list. > m1 < matrix(1:12, nrow = 3) The dimnames() command retrieves the names like so: > dimnames(m1) [[1]] [1] "Jan" "Feb" "Mar" [[2]]
[1] "Apr" "May" "Jun" "Jul"
Notice the double square braces acting as element names. You can get one element by using the square brace notation: > dimnames(m1)[1] [[1]] [1] "Jan" "Feb" "Mar" > dimnames(m1)[[1]] [1] "Jan" "Feb" "Mar" When you use single braces you get the "name" of the element. If you use double braces you do not. The dimnames() command will work on matrix, array or data.frame objects. You can use the command to set a single element but you need to use the double braces to get it to work: > dimnames(m1)[[1]] < letters[1:3] Note that you cannot set a single element to NULL although you can set all elements to NULL: > dimnames(m1)[[1]] < NULL 

<< Previous Tip: Naming rows and columns of a matrix  >> Next Tip: Object elements: brackets [], double brackets [[]] and $  
Follow me... 

Top  Tips & Tricks Home  Index of R Tips  Index of Excel Tips  
More links: 
See my Publications about Excel, R, statistics and data analysis  Courses in R, data analysis, data management and statistics  Visit the R Project website  
See my Publications about statistics and data analysis. MonogRaphs: random topics in R Writer's Bloc – my latest writing project includes R scripts Courses in data analysis, data management and statistics. 


Top  Home  Contact  GardenersOwn Homepage 