Mukana lista R ohjelmointi on yleinen objekti, joka koostuu järjestetystä objektikokoelmasta. Listat ovat yksiulotteinen , heterogeeninen Tietorakenteet.
Luettelo voi olla luettelo vektorit , matriisiluettelo, merkkiluettelo, luettelo toimintoja , ja niin edelleen.
Lista on vektori, mutta siinä on heterogeenisiä tietoelementtejä. Luettelo R:ssä luodaan käyttämällä lista()-funktio .
R mahdollistaa pääsyn R-luettelon elementteihin indeksiarvon avulla. R:ssä luettelon indeksointi alkaa 1:stä 0:n sijaan.
Luettelon luominen
Listan luomiseksi R:ssä sinun on käytettävä toimintoa nimeltä lista() .
Toisin sanoen lista on yleinen vektori, joka sisältää muita objekteja. Havainnollistaaksemme, miltä luettelo näyttää, otamme esimerkin tästä. Haluamme luoda työntekijöistä luettelon yksityiskohdista. Joten tätä varten haluamme attribuutteja, kuten ID, työntekijän nimi ja työntekijöiden lukumäärä.
Esimerkki:
R
# R program to create a List> > # The first attributes is a numeric vector> # containing the employee IDs which is created> # using the command here> empId =>c>(1, 2, 3, 4)> > # The second attribute is the employee name> # which is created using this line of code here> # which is the character vector> empName =>c>(>'Debi'>,>'Sandeep'>,>'Subham'>,>'Shiba'>)> > # The third attribute is the number of employees> # which is a single numeric variable.> numberOfEmp = 4> > # We can combine all these three different> # data types into a list> # containing the details of employees> # which can be done using a list command> empList =>list>(empId, empName, numberOfEmp)> > print>(empList)> |
>
>Lähtö
[[1]] [1] 1 2 3 4 [[2]] [1] 'Debi' 'Sandeep' 'Subham' 'Shiba' [[3]] [1] 4>
Nimeämisluettelon komponentit
Nimeämisluettelokomponentit helpottavat niiden käyttöä.
muotoile päivämäärä javassa
Esimerkki:
R
# Creating a named list> my_named_list <->list>(name =>'Sudheer'>, age = 25, city =>'Delhi'>)> # Printing the named list> print>(my_named_list)> |
>
>Lähtö
$name [1] 'Sudheer' $age [1] 25 $city [1] 'Delhi'>
R-luettelon komponenttien käyttäminen
Voimme käyttää R-luettelon komponentteja kahdella tavalla.
1. Käytä komponentteja nimillä:
Kaikki listan komponentit voidaan nimetä, ja voimme käyttää näitä nimiä päästäksemme R-luettelon komponentteihin dollar-komennolla.
Esimerkki:
R
# R program to access> # components of a list> # Creating a list by naming all its components> empId =>c>(1, 2, 3, 4)> empName =>c>(>'Debi'>,>'Sandeep'>,>'Subham'>,>'Shiba'>)> numberOfEmp = 4> empList =>list>(> >'ID'> = empId,> >'Names'> = empName,> >'Total Staff'> = numberOfEmp> >)> print>(empList)> # Accessing components by names> cat>(>'Accessing name components using $ command
'>)> print>(empList$Names)> |
>
>Lähtö
$ID [1] 1 2 3 4 $Names [1] 'Debi' 'Sandeep' 'Subham' 'Shiba' $`Total Staff` [1] 4 Accessing name components using $ command [1] 'Debi' 'Sandeep' 'Subham' 'Shiba'>
2. Käytä komponentteja indeksien avulla:
Pääsemme myös R-listan komponentteihin indeksien avulla.
Päästäksemme R-luettelon ylimmän tason komponentteihin meidän on käytettävä kaksoisviipalointioperaattoria [[ ]] joka on kaksi hakasulkua ja jos haluamme käyttää R-luettelon alemman tai sisäisen tason komponentteja, meidän on käytettävä toista hakasuljetta [ ] sekä kaksoisviipalointioperaattori [[ ]] .
Esimerkki:
R
# R program to access> # components of a list> # Creating a list by naming all its components> empId =>c>(1, 2, 3, 4)> empName =>c>(>'Debi'>,>'Sandeep'>,>'Subham'>,>'Shiba'>)> numberOfEmp = 4> empList =>list>(> >'ID'> = empId,> >'Names'> = empName,> >'Total Staff'> = numberOfEmp> >)> print>(empList)> # Accessing a top level components by indices> cat>(>'Accessing name components using indices
'>)> print>(empList[[2]])> # Accessing a inner level components by indices> cat>(>'Accessing Sandeep from name using indices
'>)> print>(empList[[2]][2])> # Accessing another inner level components by indices> cat>(>'Accessing 4 from ID using indices
'>)> print>(empList[[1]][4])> |
>
>Lähtö
$ID [1] 1 2 3 4 $Names [1] 'Debi' 'Sandeep' 'Subham' 'Shiba' $`Total Staff` [1] 4 Accessing name components using indices [1] 'Debi' 'Sandeep' 'Subham' 'Shiba' Accessing Sandeep from na...>
Luettelon osien muokkaaminen
R-luetteloa voidaan myös muokata avaamalla komponentit ja korvaamalla ne haluamillasi.
Esimerkki:
R
# R program to edit> # components of a list> # Creating a list by naming all its components> empId =>c>(1, 2, 3, 4)> empName =>c>(>'Debi'>,>'Sandeep'>,>'Subham'>,>'Shiba'>)> numberOfEmp = 4> empList =>list>(> >'ID'> = empId,> >'Names'> = empName,> >'Total Staff'> = numberOfEmp> )> cat>(>'Before modifying the list
'>)> print>(empList)> # Modifying the top-level component> empList$`Total Staff` = 5> # Modifying inner level component> empList[[1]][5] = 5> empList[[2]][5] =>'Kamala'> cat>(>'After modified the list
'>)> print>(empList)> |
>
>Lähtö
Before modifying the list $ID [1] 1 2 3 4 $Names [1] 'Debi' 'Sandeep' 'Subham' 'Shiba' $`Total Staff` [1] 4 After modified the list $ID [1] 1 2 3 4 5 $Names [1] 'Debi' 'Sandeep' 'Subham' ...>
Listojen ketjuttaminen
Kaksi R-listaa voidaan ketjuttaa käyttämällä ketjutusfunktiota. Joten kun haluamme ketjuttaa kaksi listaa, meidän on käytettävä ketjutusoperaattoria.
Syntaksi:
lista = c(luettelo, lista1)
lista = alkuperäinen lista
lista1 = uusi luettelo
Esimerkki:
R
# R program to edit> # components of a list> # Creating a list by naming all its components> empId =>c>(1, 2, 3, 4)> empName =>c>(>'Debi'>,>'Sandeep'>,>'Subham'>,>'Shiba'>)> numberOfEmp = 4> empList =>list>(> >'ID'> = empId,> >'Names'> = empName,> >'Total Staff'> = numberOfEmp> )> cat>(>'Before concatenation of the new list
'>)> print>(empList)> # Creating another list> empAge =>c>(34, 23, 18, 45)> # Concatenation of list using concatenation operator> empList =>c>(empName, empAge)> cat>(>'After concatenation of the new list
'>)> print>(empList)> |
>
rivi ja sarake
>Lähtö
Before concatenation of the new list $ID [1] 1 2 3 4 $Names [1] 'Debi' 'Sandeep' 'Subham' 'Shiba' $`Total Staff` [1] 4 After concatenation of the new list [1] 'Debi' 'Sandeep' 'Subham' 'S...>
Kohteen lisääminen luetteloon
Voit lisätä kohteen luettelon loppuun käyttämällä append()-funktiota.
R
# creating a list> my_numbers =>c>(1,5,6,3)> #adding number at the end of list> append>(my_numbers, 45)> #printing list> my_numbers> |
>
>Lähtö
[1] 1 5 6 3 45 [1] 1 5 6 3>
Luettelon osien poistaminen
R-luettelon komponenttien poistamiseksi meidän on ensin päästävä näihin komponentteihin ja lisättävä sitten negatiivinen merkki ennen näitä komponentteja. Se osoittaa, että meidän oli poistettava kyseinen komponentti.
Esimerkki:
R
# R program to access> # components of a list> # Creating a list by naming all its components> empId =>c>(1, 2, 3, 4)> empName =>c>(>'Debi'>,>'Sandeep'>,>'Subham'>,>'Shiba'>)> numberOfEmp = 4> empList =>list>(> >'ID'> = empId,> >'Names'> = empName,> >'Total Staff'> = numberOfEmp> )> cat>(>'Before deletion the list is
'>)> print>(empList)> # Deleting a top level components> cat>(>'After Deleting Total staff components
'>)> print>(empList[-3])> # Deleting a inner level components> cat>(>'After Deleting sandeep from name
'>)> print>(empList[[2]][-2])> |
>
>Lähtö
Before deletion the list is $ID [1] 1 2 3 4 $Names [1] 'Debi' 'Sandeep' 'Subham' 'Shiba' $`Total Staff` [1] 4 After Deleting Total staff components $ID [1] 1 2 3 4 $Names [1] 'Debi' 'Sand...>
Listan yhdistäminen
Voimme yhdistää R-luettelon sijoittamalla kaikki luettelot yhdeksi luetteloksi.
R
# Create two lists.> lst1 <->list>(1,2,3)> lst2 <->list>(>'Sun'>,>'Mon'>,>'Tue'>)> # Merge the two lists.> new_list <->c>(lst1,lst2)> # Print the merged list.> print>(new_list)> |
>
>
Lähtö:
[[1]] [1] 1 [[2]] [1] 2 [[3]] [1] 3 [[4]] [1] 'Sun' [[5]] [1] 'Mon' [[6]] [1] 'Tue'>
Listan muuntaminen vektoriksi
Tässä aiomme muuntaa R-luettelon vektoriksi, tätä varten luomme ensin luettelon ja poistamme luettelon sitten vektoriksi.
R
# Create lists.> lst <->list>(1:5)> print>(lst)> # Convert the lists to vectors.> vec <->unlist>(lst)> print>(vec)> |
>
if-lause java
>Lähtö
[[1]] [1] 1 2 3 4 5 [1] 1 2 3 4 5>
R Lista matriisiin
Luomme matriiseja R-ohjelmoinnin matrix()-funktiolla. Toinen käytettävä funktio on unlist()-funktio, joka muuntaa listat vektoreiksi.
R
# Defining list> lst1 <->list>(>list>(1, 2, 3),> >list>(4, 5, 6))> # Print list> cat>(>'The list is:
'>)> print>(lst1)> cat>(>'Class:'>,>class>(lst1),>'
'>)> # Convert list to matrix> mat <->matrix>(>unlist>(lst1), nrow = 2, byrow =>TRUE>)> # Print matrix> cat>(>'
After conversion to matrix:
'>)> print>(mat)> cat>(>'Class:'>,>class>(mat),>'
'>)> |
>
>Lähtö
The list is: [[1]] [[1]][[1]] [1] 1 [[1]][[2]] [1] 2 [[1]][[3]] [1] 3 [[2]] [[2]][[1]] [1] 4 [[2]][[2]] [1] 5 [[2]][[3]] [1] 6 Class: list After conversion to matrix: [,1] [,2] [,3] [1,...>
Tässä artikkelissa olemme käsitelleet Luettelot kirjassa R, Olemme käsitelleet luettelotoiminnot, kuten luettelon luominen, nimeäminen, yhdistäminen ja poistaminen R-kielellä. R-lista on tärkeä käsite, eikä sitä pidä ohittaa.
Toivottavasti opit R-luetteloista ja niiden toiminnoista tässä artikkelissa.
Tarkista myös:
- R-Array
- R- Tuples
- R – Matriisit