R-matrica yra dvimatis duomenų išdėstymas eilutėse ir stulpeliuose.
Matricoje eilutės yra tos, kurios eina horizontaliai, o stulpeliai yra tos, kurios eina vertikaliai. Į R programavimas , matricos yra dvimatės, vienalytės duomenų struktūros. Štai keletas matricų pavyzdžių:

R – Matricos
Matricos kūrimas R
Norėdami sukurti matricą R, turite naudoti funkciją, vadinamą matrica() .
Argumentai dėl to matrica() yra vektoriaus elementų rinkinys. Turite perduoti, kiek eilučių ir kiek stulpelių skaičių norite turėti savo matricoje.
Pastaba: Pagal numatytuosius nustatymus matricos yra stulpelių tvarka.
Sintaksė, skirta sukurti R matricą
matrica (duomenys, nrow, ncol, pagal eilę, maži vardai)
Parametrai:
- duomenys – vertes, kurias norite įvesti
- nrow - ne. eilučių
- ncol – ne. stulpelių
- šalia - loginis užuominas, jei „true“ reikšmė bus priskirta eilutėmis
- vardai - eilučių ir stulpelių pavadinimai
Pavyzdys:
pakeisti pavadinimą katalogo linux
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)> |
>
>Išvestis
The 3x3 matrix: c d e a 1 2 3 b 4 5 6 c 7 8 9>
Specialiųjų matricų kūrimas R
R leidžia kurti įvairių tipų matricas, naudojant argumentus, perduodamus funkcijai matrica().
1. Matrica, kurioje visos eilutės ir stulpeliai užpildyti viena konstanta „k“:
Norint sukurti tokią R matricą, sintaksė pateikiama žemiau:
Sintaksė: matrica(k, m, n)
Parametrai:
k: konstanta
m: eilučių nėra
n: stulpelių skaičius
Pavyzdys:
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))> |
>
>Išvestis
[,1] [,2] [,3] [1,] 5 5 5 [2,] 5 5 5 [3,] 5 5 5>
2. Įstrižainė matrica:
Įstrižainės matrica yra matrica, kurioje visi įrašai už pagrindinės įstrižainės yra nuliai. Norint sukurti tokią R matricą, sintaksė pateikiama žemiau:
Sintaksė: diag(k, m, n)
Parametrai:
k: konstantos/masyvas
m: eilučių nėra
n: stulpelių skaičius
Pavyzdys:
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))> |
>
>Išvestis
[,1] [,2] [,3] [1,] 5 0 0 [2,] 0 3 0 [3,] 0 0 3>
3. Tapatybės matrica:
Tapatybės matrica, kurioje visi pagrindinės įstrižainės elementai yra vienetai, o visi kiti – nuliai. Norint sukurti tokią R matricą, sintaksė pateikiama žemiau:
Sintaksė: diag(k, m, n)
Parametrai:
k: 1
m: eilučių nėra
n: stulpelių skaičius
Pavyzdys:
R
# R program to illustrate> # special matrices> # Identity matrix having> # 3 rows and 3 columns> print>(>diag>(1, 3, 3))> |
>
>Išvestis
[,1] [,2] [,3] [1,] 1 0 0 [2,] 0 1 0 [3,] 0 0 1>
4. Matricos metrika
Matricos metrika pasakoja apie jūsų sukurtą Matricą. Galbūt norėsite sužinoti eilučių skaičių, stulpelių skaičių, matricos matmenis.
Žemiau pateiktas pavyzdys padės atsakyti į šiuos klausimus:
- Kaip sužinoti matricos matmenis?
- Kaip sužinoti, kiek eilučių yra matricoje?
- Kiek stulpelių yra matricoje?
- Kiek elementų yra matricoje?
Pavyzdys:
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)))> |
>
>Išvestis
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] ...>
Prieiga prie R-Matrix elementų
R matricų elementus galime pasiekti naudodami tą pačią tvarką, kurios laikomasi duomenų rėmeliuose. Taigi, turėsite matricą, po kurios bus laužtiniai skliaustai su kableliu tarp masyvo.
Reikšmė prieš kablelį naudojama eilėms pasiekti, o reikšmė, esanti po kablelio, naudojama pasiekti stulpelius. Pavaizduokime tai paimdami paprastą R kodą.
Prieiga prie eilučių:
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, ])> |
np paminkštinimas
>
>Išvestis
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>
Prieiga prie stulpelių:
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])> |
>
>Išvestis
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>
Daugiau prieigos prie R matricos elementų pavyzdys:
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])> |
>
>Išvestis
The 3x3 matrix: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 [1] 2 [1] 6>
Prieiga prie pomatricų R:
Mes galime pasiekti pomatricą matricoje naudodami dvitaškis(:) operatorius.
R
gimp kaip panaikinti pasirinkimą
# 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])> |
>
>Išvestis
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...>
R-matricos elementų modifikavimas
R galite modifikuoti matricų elementus tiesioginiu priskyrimu.
Pavyzdys:
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)> |
>
>Išvestis
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>
R-Matricos sujungimas
Matricos sujungimas reiškia esamos R matricos eilučių arba stulpelių sujungimą.
Eilutės sujungimas:
Eilutės sujungimas su matrica atliekamas naudojant 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)> |
d šlepetės
>
>Išvestis
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...>
Stulpelio sujungimas:
Stulpelio sujungimas su matrica atliekamas naudojant 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)> |
>
>Išvestis
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] ...>
Matmenų neatitikimas: Atminkite, kad prieš atlikdami šią matricos sujungimą, turite įsitikinti, kad matricos matmenys yra nuoseklūs.
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)> |
>
>
Išvestis:
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)>
Eilučių ir stulpelių įtraukimas į R matricą
Norėdami pridėti eilutę į R matricą, galite naudoti rbind() funkcija ir pridėti stulpelį prie R matricos, kurią galite naudoti cbind () funkcija.
Pridedama eilutė
Pažiūrėkime toliau pateiktą pavyzdį, kaip pridėti eilutę R matricoje?
Pavyzdys:
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)> |
>
>Išvestis
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,]...>
Stulpelio pridėjimas
Pažiūrėkime žemiau pavyzdį, kaip pridėti stulpelį R matricoje?
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)> |
>
>Išvestis
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...>
R-matricos eilučių ir stulpelių ištrynimas
Norėdami ištrinti eilutę arba stulpelį, pirmiausia turite pasiekti tą eilutę arba stulpelį ir prieš tą eilutę ar stulpelį įterpti neigiamą ženklą. Tai rodo, kad turėjote ištrinti tą eilutę arba stulpelį.
Eilutės ištrynimas:
R
css centrinis mygtukas
# 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)> |
>
>Išvestis
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>
Stulpelio ištrynimas:
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)> |
>
>Išvestis
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>
Aptarėme R matricas ir pagrindines jų operacijas, tokias kaip naujų eilučių ir stulpelių pridėjimas, eilučių ir stulpelių trynimas, matricų sujungimas ir kt.
Tikimės, kad tai padėjo jums suprasti R matricas ir dabar galite patogiai naudoti R matricas savo projektuose.
Taip pat patikrinkite:
- R – masyvas
- R – sąrašai
- R – Tuples