Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Labeling Rows and Columns | Section
Practice
Projects
Quizzes & Challenges
Cuestionarios
Challenges
/
Essential R Programming for Absolute Beginners - 1768563985826

bookLabeling Rows and Columns

Instead of referring to matrix elements by numeric indices, you can assign names to rows and columns. This makes large matrices easier to read and navigate.

Assigning Names

You can use rownames() and colnames() functions to add labels to rows and columns.

Example

12345678
m <- matrix(1:9, nrow = 3, byrow = TRUE) # Assign row names rownames(m) <- c("r1", "r2", "r3") # Assign column names colnames(m) <- c("c1", "c2", "c3") m
copy
Note
Note

The number of names must match the number of rows or columns in the matrix.

Accessing by Names

Once names are assigned, you can extract elements or entire rows/columns using them.

Example

123456789
num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) rownames(m) <- c('r1', 'r2', 'r3') colnames(m) <- c('c1', 'c2', 'c3') # Extract element at row "r2", column "c1" (value 4) m["r2", "c1"] # Extract the entire first row m["r1",]
copy

Using names instead of indices makes code more readable and less error-prone.

Tarea

Swipe to start coding

You are given a matrix sellings that stores sales data for a local furniture store across three months:

MonthSofaArmchairDining tableDining chairBookshelf
March1621302310
April4039132116
May1121363216

The matrix currently lacks row and column names.

Your tasks are to:

  1. Assign c("March", "April", "May") to row names of sellings.
  2. Assign c("Sofa", "Armchair, "Dining_table", "Dining_chair", "Bookshelf") to column names. Pay attention: use underscore (_) characters instead of spaces.
  3. Output matrix sellings.

Solución

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 31
single

single

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

close

bookLabeling Rows and Columns

Desliza para mostrar el menú

Instead of referring to matrix elements by numeric indices, you can assign names to rows and columns. This makes large matrices easier to read and navigate.

Assigning Names

You can use rownames() and colnames() functions to add labels to rows and columns.

Example

12345678
m <- matrix(1:9, nrow = 3, byrow = TRUE) # Assign row names rownames(m) <- c("r1", "r2", "r3") # Assign column names colnames(m) <- c("c1", "c2", "c3") m
copy
Note
Note

The number of names must match the number of rows or columns in the matrix.

Accessing by Names

Once names are assigned, you can extract elements or entire rows/columns using them.

Example

123456789
num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) rownames(m) <- c('r1', 'r2', 'r3') colnames(m) <- c('c1', 'c2', 'c3') # Extract element at row "r2", column "c1" (value 4) m["r2", "c1"] # Extract the entire first row m["r1",]
copy

Using names instead of indices makes code more readable and less error-prone.

Tarea

Swipe to start coding

You are given a matrix sellings that stores sales data for a local furniture store across three months:

MonthSofaArmchairDining tableDining chairBookshelf
March1621302310
April4039132116
May1121363216

The matrix currently lacks row and column names.

Your tasks are to:

  1. Assign c("March", "April", "May") to row names of sellings.
  2. Assign c("Sofa", "Armchair, "Dining_table", "Dining_chair", "Bookshelf") to column names. Pay attention: use underscore (_) characters instead of spaces.
  3. Output matrix sellings.

Solución

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 31
single

single

some-alt