R Set Row & Column Names of Data with Unknown Dimension (Example)

 

In this article you’ll learn how to change row and column names of a data frame or matrix without knowing the dimensions in the R programming language.

The post consists of this information:

Let’s dive into it:

 

Creation of Example Data

The first step is to create some example data:

data <- matrix(1:20, ncol = 5)                   # Create example matrix
data                                             # Print example matrix

 

table 1 matrix set row and column names without dimension r

 

Table 1 shows the structure of the example data – It is made up of four observations and five columns. However, let’s assume that we don’t know these numbers of rows and columns.

 

Example 1: Set Row Names of Data without Knowing the Number of Rows

The following R code illustrates how to set the row numbers of a matrix from 1 to the number of rows without actually knowing the number of rows.

For this, we can use the rownames and nrow functions as shown below:

rownames(data) <- 1:nrow(data)                   # Set row names
data                                             # Print updated data

 

table 2 matrix set row and column names without dimension r

 

As shown in Table 2, we have set the row names of our matrix by executing the previously shown R programming syntax.

The column names are still not specified though…

 

Example 2: Set Column Names of Data without Knowing the Number of Columns

The syntax below explains how to define variable names of an unknown number of columns.

For this task, we can apply the colnames, paste0, and ncol functions as shown below:

colnames(data) <- paste0("Col", 1:ncol(data))    # Set column names
data                                             # Print updated data

 

table 3 matrix set row and column names without dimension r

 

As shown in Table 3, the previously shown R syntax has created a matrix with manually specified column names even though we didn’t explicitly specify the dimensions of our data.

 

Video, Further Resources & Summary

I have recently published a video instruction on my YouTube channel, which explains the contents of this tutorial. You can find the video below:

 

 

Furthermore, you might read the other articles on this homepage.

 

On this page you have learned how to set row and column names without knowing the number of rows and columns in R programming.

In this tutorial, we have changed the row and column names of a matrix object. However, please note that we could apply the same syntax to a data frame object as well.

Let me know in the comments, in case you have any further questions.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top