The rbind() function in R conveniently adds the names of the vectors to the rows of the matrix. You name the values in a vector, and you can do something very similar with rows and columns in a matrix.
For that, you have the functions rownames() and colnames(). Guess which one does what? Both functions work much like the names() function you use when naming vector values.
WithColumnRenamed Description. Rename an existing column in a DataFrame. Rename an existing column in a DataFrame. Usage ## S4 method for signature 'DataFrame,character,character' withColumnRenamed(x, existingCol, newCol) ## S4 method for signature 'DataFrame' rename(x.) rename(x.) withColumnRenamed(x, existingCol, newCol) Arguments.
Changing the row and column names
The matrix baskets.team already has some row names. It would be better if the names of the rows would just read “Granny” and “Geraldine”. You can easily change these row names like this:
You can look at the matrix to check if this did what it’s supposed to do, or you can take a look at the row names itself like this:
The colnames() function works exactly the same. You can, for example, add the number of the game as a column name using the following code:
This gives you the following matrix:
This is almost like you want it, but the third column name contains an annoying writing mistake. No problem there, R allows you to easily correct that mistake. Just as the with names() function, you can use indices to extract or to change a specific row or column name. You can correct the mistake in the column names like this:
If you want to get rid of either column names or row names, the only thing you need to do is set their value to NULL. This also works for vector names, by the way. You can try that out yourself on a copy of the matrix baskets.team like this:
![R rename column data table R rename column data table](/uploads/1/2/6/2/126286776/785016211.png)
R stores the row and column names in an attribute called dimnames. Use the dimnames() function to extract or set those values.
Using names as indices
These row and column names can be used just like you use names for values in a vector. You can use these names instead of the index number to select values from a vector. This works for matrices as well, using the row and column names.
Say you want to select the second and the fifth game for both ladies, try:
Exactly as before, you get all rows if you don’t specify which ones you want. Alternatively, you can extract all the results for Granny like this:
That’s the result, indeed, but the row name is gone now. R tries to simplify the matrix to a vector, if that’s possible. In this case, a single row is returned so, by default, this result is transformed to a vector.
If a one-row matrix is simplified to a vector, the column names are used as names for the values. If a one-column matrix is simplified to a vector, the row names are used as names for the vector. If you want to keep all names, you must set the argument drop to FALSE to avoid conversion to a vector.