R – Macierze
Macierz R to dwuwymiarowy układ danych w wierszach i kolumnach.
W macierzy wiersze biegną poziomo, a kolumny pionowo. W Programowanie R , macierze są dwuwymiarowymi, jednorodnymi strukturami danych. Oto kilka przykładów macierzy:
R – Macierze
Tworzenie macierzy w R
Aby utworzyć macierz w R, należy skorzystać z funkcji o nazwie matryca() .
Argumenty na to matryca() są zbiorem elementów wektora. Musisz podać liczbę wierszy i liczbę kolumn, które chcesz mieć w swojej macierzy.
Notatka: Domyślnie macierze są ułożone w kolejności kolumnowej.
Składnia tworzenia macierzy R
matrix(dane, nrow, ncol, byrow, dimnames)
Parametry:
- dane - wartości, które chcesz wprowadzić
- nrów – NIE. rzędów
- podoficer – NIE. kolumn
- byrow – logiczna wskazówka, jeśli wiersze przypisują wartość „true”.
- dimnames – nazwy wierszy i kolumn
Przykład:
R
# R program to create a matrix> > A => matrix> (> > > # Taking sequence of elements> > c> (1, 2, 3, 4, 5, 6, 7, 8, 9),> > > # No of rows> > nrow = 3,> > > # No of columns> > ncol = 3,> > > # By default matrices are in column-wise order> > # So this parameter decides how to arrange the matrix> > byrow => TRUE> )> > # Naming rows> rownames> (A) => c> (> 'a'> ,> 'b'> ,> 'c'> )> > # Naming columns> colnames> (A) => c> (> 'c'> ,> 'd'> ,> 'e'> )> > cat> (> 'The 3x3 matrix:
'> )> print> (A)> |
Wyjście
The 3x3 matrix: c d e a 1 2 3 b 4 5 6 c 7 8 9
Tworzenie specjalnych macierzy w R
R umożliwia tworzenie różnego rodzaju macierzy za pomocą argumentów przekazywanych do funkcji matrix().
1. Macierz, w której wszystkie wiersze i kolumny są wypełnione jedną stałą „k”:
Aby utworzyć taką macierz R, składnię podano poniżej:
Składnia: macierz (k, m, n)
Parametry:
k: stała
M: liczba rzędów
N: liczba kolumn
Przykład:
R
# R program to illustrate> # special matrices> # Matrix having 3 rows and 3 columns> # filled by a single constant 5> print> (> matrix> (5, 3, 3))> |
Wyjście
[,1] [,2] [,3] [1,] 5 5 5 [2,] 5 5 5 [3,] 5 5 5
2. Macierz diagonalna:
Macierz diagonalna to macierz, w której wszystkie elementy poza główną przekątną mają wartość zerową. Aby utworzyć taką macierz R, składnię podano poniżej:
Składnia: diag(k, m, n)
Parametry:
k: stałe/tablica
M: liczba rzędów
N: liczba kolumn
Przykład:
R
# R program to illustrate> # special matrices> # Diagonal matrix having 3 rows and 3 columns> # filled by array of elements (5, 3, 3)> print> (> diag> (> c> (5, 3, 3), 3, 3))> |
Wyjście
[,1] [,2] [,3] [1,] 5 0 0 [2,] 0 3 0 [3,] 0 0 3
3. Macierz jednostkowa:
Macierz jednostkowa, w której wszystkie elementy głównej przekątnej są jedynkami, a wszystkie pozostałe elementy są zerami. Aby utworzyć taką macierz R, składnię podano poniżej:
Składnia: diag(k, m, n)
Parametry:
k: 1
M: liczba rzędów
N: liczba kolumn
Przykład:
R
# R program to illustrate> # special matrices> # Identity matrix having> # 3 rows and 3 columns> print> (> diag> (1, 3, 3))> |
Wyjście
[,1] [,2] [,3] [1,] 1 0 0 [2,] 0 1 0 [3,] 0 0 1
4. Metryki matrycowe
Metryki macierzy informują o utworzonej macierzy. Możesz chcieć znać liczbę wierszy, liczbę kolumn, wymiary macierzy.
Poniższy przykład pomoże Ci odpowiedzieć na następujące pytania:
- Skąd możesz znać wymiar macierzy?
- Skąd możesz wiedzieć, ile wierszy jest w macierzy?
- Ile kolumn znajduje się w macierzy?
- Ile elementów jest w macierzy?
Przykład:
R
# R program to illustrate> # matrix metrics> # Create a 3x3 matrix> A => matrix> (> > c> (1, 2, 3, 4, 5, 6, 7, 8, 9),> > nrow = 3,> > ncol = 3,> > byrow => TRUE> )> cat> (> 'The 3x3 matrix:
'> )> print> (A)> cat> (> 'Dimension of the matrix:
'> )> print> (> dim> (A))> cat> (> 'Number of rows:
'> )> print> (> nrow> (A))> cat> (> 'Number of columns:
'> )> print> (> ncol> (A))> cat> (> 'Number of elements:
'> )> print> (> length> (A))> # OR> print> (> prod> (> dim> (A)))> |
Wyjście
The 3x3 matrix: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 Dimension of the matrix: [1] 3 3 Number of rows: [1] 3 Number of columns: [1] 3 Number of elements: [1] ...
Dostęp do elementów macierzy R
Dostęp do elementów macierzy R możemy uzyskać stosując tę samą konwencję, jaka jest stosowana w ramkach danych. Będziesz miał macierz, po której nastąpi nawias kwadratowy z przecinkiem pomiędzy tablicą.
Wartość przed przecinkiem umożliwia dostęp do wierszy, a wartość znajdująca się po przecinku umożliwia dostęp do kolumn. Zilustrujmy to, biorąc prosty kod R.
Dostęp do wierszy:
R
# R program to illustrate> # access rows in metrics> # Create a 3x3 matrix> A => matrix> (> > c> (1, 2, 3, 4, 5, 6, 7, 8, 9),> > nrow = 3,> > ncol = 3,> > byrow => TRUE> )> cat> (> 'The 3x3 matrix:
'> )> print> (A)> # Accessing first and second row> cat> (> 'Accessing first and second row
'> )> print> (A[1:2, ])> |
Wyjście
The 3x3 matrix: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 Accessing first and second row [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6
Dostęp do kolumn:
R
# R program to illustrate> # access columns in metrics> # Create a 3x3 matrix> A => matrix> (> > c> (1, 2, 3, 4, 5, 6, 7, 8, 9),> > nrow = 3,> > ncol = 3,> > byrow => TRUE> )> cat> (> 'The 3x3 matrix:
'> )> print> (A)> # Accessing first and second column> cat> (> 'Accessing first and second column
'> )> print> (A[, 1:2])> |
Wyjście
The 3x3 matrix: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 Accessing first and second column [,1] [,2] [1,] 1 2 [2,] 4 5 [3,] 7 8
Więcej przykładów dostępu do elementów macierzy R:
R
# R program to illustrate> # access an entry in metrics> # Create a 3x3 matrix> A => matrix> (> > c> (1, 2, 3, 4, 5, 6, 7, 8, 9),> > nrow = 3,> > ncol = 3,> > byrow => TRUE> )> cat> (> 'The 3x3 matrix:
'> )> print> (A)> # Accessing 2> print> (A[1, 2])> # Accessing 6> print> (A[2, 3])> |
Wyjście
The 3x3 matrix: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 [1] 2 [1] 6
Dostęp do podmacierzy w R:
Dostęp do podmacierzy w macierzy możemy uzyskać za pomocą okrężnica(:) operator.
R
# R program to illustrate> # access submatrices in a matrix> # Create a 3x3 matrix> A => matrix> (> > c> (1, 2, 3, 4, 5, 6, 7, 8, 9),> > nrow = 3,> > ncol = 3,> > byrow => TRUE> )> cat> (> 'The 3x3 matrix:
'> )> print> (A)> cat> (> 'Accessing the first three rows and the first two columns
'> )> print> (A[1:3, 1:2])> |
Wyjście
The 3x3 matrix: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 Accessing the first three rows and the first two columns [,1] [,2] [1,] 1 2 [2,] 4 5 [3...
Modyfikowanie elementów macierzy R
W R możesz modyfikować elementy macierzy poprzez bezpośrednie przypisanie.
Przykład:
R
# R program to illustrate> # editing elements in metrics> # Create a 3x3 matrix> A => matrix> (> > c> (1, 2, 3, 4, 5, 6, 7, 8, 9),> > nrow = 3,> > ncol = 3,> > byrow => TRUE> )> cat> (> 'The 3x3 matrix:
'> )> print> (A)> # Editing the 3rd rows and 3rd column element> # from 9 to 30> # by direct assignments> A[3, 3] = 30> cat> (> 'After edited the matrix
'> )> print> (A)> |
Wyjście
The 3x3 matrix: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 After edited the matrix [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 30
Konkatenacja macierzy R
Konkatenacja macierzy odnosi się do łączenia wierszy lub kolumn istniejącej macierzy R.
Łączenie wiersza:
Łączenie wiersza z macierzą odbywa się za pomocą rbind() .
R
# R program to illustrate> # concatenation of a row in metrics> # Create a 3x3 matrix> A => matrix> (> > c> (1, 2, 3, 4, 5, 6, 7, 8, 9),> > nrow = 3,> > ncol = 3,> > byrow => TRUE> )> cat> (> 'The 3x3 matrix:
'> )> print> (A)> # Creating another 1x3 matrix> B => matrix> (> > c> (10, 11, 12),> > nrow = 1,> > ncol = 3> )> cat> (> 'The 1x3 matrix:
'> )> print> (B)> # Add a new row using rbind()> C => rbind> (A, B)> cat> (> 'After concatenation of a row:
'> )> print> (C)> |
Wyjście
The 3x3 matrix: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 The 1x3 matrix: [,1] [,2] [,3] [1,] 10 11 12 After concatenation of a row: [,1] [,2] [,3...
Łączenie kolumny:
Łączenie kolumny z macierzą odbywa się za pomocą cbind() .
R
# R program to illustrate> # concatenation of a column in metrics> # Create a 3x3 matrix> A => matrix> (> > c> (1, 2, 3, 4, 5, 6, 7, 8, 9),> > nrow = 3,> > ncol = 3,> > byrow => TRUE> )> cat> (> 'The 3x3 matrix:
'> )> print> (A)> # Creating another 3x1 matrix> B => matrix> (> > c> (10, 11, 12),> > nrow = 3,> > ncol = 1,> > byrow => TRUE> )> cat> (> 'The 3x1 matrix:
'> )> print> (B)> # Add a new column using cbind()> C => cbind> (A, B)> cat> (> 'After concatenation of a column:
'> )> print> (C)> |
Wyjście
The 3x3 matrix: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 The 3x1 matrix: [,1] [1,] 10 [2,] 11 [3,] 12 After concatenation of a column: [,1] [,2] ...
Niespójność wymiarów: Pamiętaj, że przed wykonaniem łączenia macierzy musisz upewnić się, że wymiary są spójne.
R
# R program to illustrate> # Dimension inconsistency in metrics concatenation> # Create a 3x3 matrix> A => matrix> (> > c> (1, 2, 3, 4, 5, 6, 7, 8, 9),> > nrow = 3,> > ncol = 3,> > byrow => TRUE> )> cat> (> 'The 3x3 matrix:
'> )> print> (A)> # Creating another 1x3 matrix> B => matrix> (> > c> (10, 11, 12),> > nrow = 1,> > ncol = 3,> )> cat> (> 'The 1x3 matrix:
'> )> print> (B)> # This will give an error> # because of dimension inconsistency> C => cbind> (A, B)> cat> (> 'After concatenation of a column:
'> )> print> (C)> |
Wyjście:
The 3x3 matrix: [, 1] [, 2] [, 3] [1, ] 1 2 3 [2, ] 4 5 6 [3, ] 7 8 9 The 1x3 matrix: [, 1] [, 2] [, 3] [1, ] 10 11 12 Error in cbind(A, B) : number of rows of matrices must match (see arg 2)
Dodawanie wierszy i kolumn w macierzy R
Aby dodać wiersz w macierzy R, możesz użyć rbind() funkcję i dodać kolumnę do macierzy R, której możesz użyć cbind () funkcja.
Dodawanie wiersza
Zobaczmy poniższy przykład, jak dodać wiersz w macierzy R?
Przykład:
R
# Create a 3x3 matrix> number <-> matrix> (> > c> (1, 2, 3, 4, 5, 6, 7, 8, 9),> > nrow = 3,> > ncol = 3,> > byrow => TRUE> )> cat> (> 'Before inserting a new row:
'> )> print> (number)> # New row to be inserted> new_row <-> c> (10, 11, 12)> # Define the new row> # Inserting the new row at the second position> A <-> rbind> (number[1, ], new_row, number[-1, ])> cat> (> '
After inserting a new row:
'> )> print> (number)> |
Wyjście
Before inserting a new row: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 After inserting a new row: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,]...
Dodawanie kolumny
Zobaczmy poniższy przykład, jak dodać kolumnę w macierzy R?
R
# Create a 3x3 matrix> number <-> matrix> (> > c> (1, 2, 3, 4, 5, 6, 7, 8, 9),> > nrow = 3,> > ncol = 3,> > byrow => TRUE> )> cat> (> 'Before adding a new column:
'> )> print> (number)> # New column to be added> new_column <-> c> (10, 11, 12)> # Define the new column> # Adding the new column at the end> number <-> cbind> (number, new_column)> cat> (> '
After adding a new column:
'> )> print> (number)> |
Wyjście
Before adding a new column: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 After adding a new column: new_column [1,] 1 2 3 10 [2,] 4 5 6 1...
Usuwanie wierszy i kolumn macierzy R
Aby usunąć wiersz lub kolumnę, najpierw musisz uzyskać dostęp do tego wiersza lub kolumny, a następnie wstawić znak minus przed tym wierszem lub kolumną. Wskazuje, że trzeba było usunąć ten wiersz lub kolumnę.
Usunięcie wiersza:
R
# R program to illustrate> # row deletion in metrics> # Create a 3x3 matrix> A => matrix> (> > c> (1, 2, 3, 4, 5, 6, 7, 8, 9),> > nrow = 3,> > ncol = 3,> > byrow => TRUE> )> cat> (> 'Before deleting the 2nd row
'> )> print> (A)> # 2nd-row deletion> A = A[-2, ]> cat> (> 'After deleted the 2nd row
'> )> print> (A)> |
Wyjście
Before deleting the 2nd row [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 After deleted the 2nd row [,1] [,2] [,3] [1,] 1 2 3 [2,] 7 8 9
Usunięcie kolumny:
R
# R program to illustrate> # column deletion in metrics> # Create a 3x3 matrix> A => matrix> (> > c> (1, 2, 3, 4, 5, 6, 7, 8, 9),> > nrow = 3,> > ncol = 3,> > byrow => TRUE> )> cat> (> 'Before deleting the 2nd column
'> )> print> (A)> # 2nd-row deletion> A = A[, -2]> cat> (> 'After deleted the 2nd column
'> )> print> (A)> |
Wyjście
Before deleting the 2nd column [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 After deleted the 2nd column [,1] [,2] [1,] 1 3 [2,] 4 6 [3,] 7 9
Omówiliśmy macierze R i ich podstawowe operacje, takie jak dodawanie nowych wierszy i kolumn, usuwanie wierszy i kolumn, łączenie macierzy itp.
Mam nadzieję, że pomogło ci to w zrozumieniu macierzy w R i możesz teraz wygodnie używać macierzy R w swoich projektach.
Sprawdź także:
- R – tablica
- R – Listy
- R – Krotki