O listă în programare R este un obiect generic format dintr-o colecție ordonată de obiecte. Listele sunt unidimensional , eterogen structuri de date.
Lista poate fi o listă de vectori , o listă de matrici, o listă de caractere, o listă de funcții , și așa mai departe.
O listă este un vector, dar cu elemente de date eterogene. O listă în R este creată cu ajutorul codului funcția list(). .
R permite accesarea elementelor unei liste R cu utilizarea valorii indexului. În R, indexarea unei liste începe cu 1 în loc de 0.
Crearea unei liste
Pentru a crea o listă în R trebuie să utilizați funcția numită listă() .
Cu alte cuvinte, o listă este un vector generic care conține alte obiecte. Pentru a ilustra cum arată o listă, luăm un exemplu aici. Dorim să construim o listă de angajați cu detalii. Deci, pentru aceasta, dorim atribute precum ID, numele angajatului și numărul de angajați.
Exemplu:
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)> |
>
>Ieșire
[[1]] [1] 1 2 3 4 [[2]] [1] 'Debi' 'Sandeep' 'Subham' 'Shiba' [[3]] [1] 4>
Componentele listei de denumiri
Componentele listei de denumire facilitează accesul la acestea.
dormi in javascript
Exemplu:
R
# Creating a named list> my_named_list <->list>(name =>'Sudheer'>, age = 25, city =>'Delhi'>)> # Printing the named list> print>(my_named_list)> |
>
>Ieșire
$name [1] 'Sudheer' $age [1] 25 $city [1] 'Delhi'>
Accesarea componentelor listei R
Putem accesa componentele unei liste R în două moduri.
1. Accesați componente după nume:
Toate componentele unei liste pot fi denumite și putem folosi acele nume pentru a accesa componentele listei R folosind comanda dollar.
Exemplu:
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)> |
>
>Ieșire
$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. Accesați componente după indici:
Putem accesa și componentele listei R folosind indici.
Pentru a accesa componentele de nivel superior ale unei liste R trebuie să folosim un operator de feliere dublă [[ ]] care sunt două paranteze pătrate și dacă dorim să accesăm componentele de nivel inferior sau interior ale unei liste R, trebuie să folosim o altă paranteză pătrată [ ] împreună cu operatorul de feliere dublă [[ ]] .
Exemplu:
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])> |
>
diferența dintre leu și tigru
>Ieșire
$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...>
Modificarea componentelor unei liste
O listă R poate fi modificată și accesând componentele și înlocuindu-le cu cele dorite.
Exemplu:
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)> |
>
>Ieșire
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' ...>
Concatenarea listelor
Două liste R pot fi concatenate folosind funcția de concatenare. Deci, atunci când dorim să concatenăm două liste, trebuie să folosim operatorul de concatenare.
Sintaxă:
lista = c(lista, lista1)
list = lista originală
list1 = noua listă
Exemplu:
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)> |
>
>Ieșire
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...>
Adăugarea unui articol la listă
Pentru a adăuga un articol la sfârșitul listei, putem folosi funcția append().
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> |
>
>Ieșire
[1] 1 5 6 3 45 [1] 1 5 6 3>
Ștergerea componentelor unei liste
Pentru a șterge componente ale unei liste R, în primul rând, trebuie să accesăm acele componente și apoi să introducem un semn negativ înaintea acelor componente. Indică faptul că a trebuit să ștergem acea componentă.
Exemplu:
R
instanță java a
# 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])> |
>
>Ieșire
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...>
Lista de îmbinare
Putem îmbina lista R plasând toate listele într-o singură listă.
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)> |
>
>
Ieșire:
[[1]] [1] 1 [[2]] [1] 2 [[3]] [1] 3 [[4]] [1] 'Sun' [[5]] [1] 'Mon' [[6]] [1] 'Tue'>
Conversia listei în vector
Aici vom converti lista R în vector, pentru aceasta vom crea mai întâi o listă și apoi vom elimina lista în vector.
R
# Create lists.> lst <->list>(1:5)> print>(lst)> # Convert the lists to vectors.> vec <->unlist>(lst)> print>(vec)> |
>
>Ieșire
[[1]] [1] 1 2 3 4 5 [1] 1 2 3 4 5>
R Listă în matrice
Vom crea matrice folosind funcția matrix() în programarea R. O altă funcție care va fi folosită este funcția unlist() pentru a converti listele într-un vector.
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),>'
'>)> |
>
>Ieșire
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,...>
În acest articol am acoperit Liste în R, am acoperit operațiuni de listă cum ar fi crearea, denumirea, îmbinarea și ștergerea unei liste în limbajul R. Lista R este un concept important și nu trebuie sărită.
Sper că ați aflat despre listele R și despre operațiunile sale în acest articol.
Verificați și:
- R-Array
- R- Tupluri
- R – Matrici