Seznam v R programiranje je generični objekt, sestavljen iz urejene zbirke objektov. Seznami so enodimenzionalno , heterogena podatkovne strukture.
Seznam je lahko seznam vektorji , seznam matrik, seznam znakov, seznam funkcije , in tako naprej.
Seznam je vektor, vendar s heterogenimi podatkovnimi elementi. Seznam v R je ustvarjen z uporabo funkcijo list(). .
R omogoča dostop do elementov seznama R z uporabo vrednosti indeksa. V R se indeksiranje seznama začne z 1 namesto z 0.
Ustvarjanje seznama
Če želite ustvariti seznam v R, morate uporabiti imenovano funkcijo seznam() .
Z drugimi besedami, seznam je generični vektor, ki vsebuje druge predmete. Za ponazoritev, kako izgleda seznam, vzamemo primer. Želimo sestaviti seznam zaposlenih s podrobnostmi. Za to torej želimo atribute, kot so ID, ime zaposlenega in število zaposlenih.
primer:
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)> |
>
>Izhod
[[1]] [1] 1 2 3 4 [[2]] [1] 'Debi' 'Sandeep' 'Subham' 'Shiba' [[3]] [1] 4>
Poimenovanje komponent seznama
Poimenovanje komponent seznama olajša dostop do njih.
vsebuje python
primer:
R
# Creating a named list> my_named_list <->list>(name =>'Sudheer'>, age = 25, city =>'Delhi'>)> # Printing the named list> print>(my_named_list)> |
>
>Izhod
$name [1] 'Sudheer' $age [1] 25 $city [1] 'Delhi'>
Dostop do komponent seznama R
Do komponent seznama R lahko dostopamo na dva načina.
1. Dostop do komponent po imenih:
Vse komponente seznama je mogoče poimenovati in ta imena lahko uporabimo za dostop do komponent seznama R z ukazom dolar.
primer:
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)> |
>
>Izhod
$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. Dostop do komponent po indeksih:
Do komponent seznama R lahko dostopamo tudi z indeksi.
Za dostop do komponent najvišje ravni seznama R moramo uporabiti operator dvojnega rezanja [[ ]] kar sta dva oglata oklepaja in če želimo dostopati do komponent nižje ali notranje ravni seznama R, moramo uporabiti drug oglati oklepaj [ ] skupaj z operaterjem dvojnega rezanja [[ ]] .
primer:
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])> |
>
>Izhod
$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...>
Spreminjanje komponent seznama
Seznam R lahko tudi spremenite tako, da dostopate do komponent in jih zamenjate s tistimi, ki jih želite.
primer:
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)> |
>
>Izhod
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' ...>
Veženje seznamov
Dva seznama R je mogoče povezati s funkcijo veriženja. Torej, ko želimo združiti dva seznama, moramo uporabiti operator za združevanje.
Sintaksa:
seznam = c(seznam, seznam1)
seznam = originalni seznam
list1 = nov seznam
primer:
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)> |
>
>Izhod
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...>
Dodajanje predmeta na seznam
Če želite dodati element na konec seznama, lahko uporabimo funkcijo append().
R
java podniz vsebuje
# 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> |
>
>Izhod
[1] 1 5 6 3 45 [1] 1 5 6 3>
Brisanje komponent seznama
Če želite izbrisati komponente seznama R, moramo najprej dostopati do teh komponent in nato pred temi komponentami vstaviti negativni znak. To pomeni, da smo morali to komponento izbrisati.
primer:
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])> |
>
>Izhod
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...>
Seznam združevanja
Seznam R lahko združimo tako, da vse sezname postavimo v en sam seznam.
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)> |
>
>
Izhod:
[[1]] [1] 1 [[2]] [1] 2 [[3]] [1] 3 [[4]] [1] 'Sun' [[5]] [1] 'Mon' [[6]] [1] 'Tue'>
Pretvarjanje seznama v vektor
Tukaj bomo pretvorili seznam R v vektor, za to bomo najprej ustvarili seznam in nato odstranili seznam v vektor.
R
# Create lists.> lst <->list>(1:5)> print>(lst)> # Convert the lists to vectors.> vec <->unlist>(lst)> print>(vec)> |
>
>Izhod
[[1]] [1] 1 2 3 4 5 [1] 1 2 3 4 5>
R Seznam v matriko
Matrike bomo ustvarili s funkcijo matrix() v programiranju R. Druga funkcija, ki bo uporabljena, je funkcija unlist() za pretvorbo seznamov v vektor.
R
prelivanje besed css
# 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),>'
'>)> |
>
>Izhod
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,...>
V tem članku smo obravnavali Seznami v R, obravnavali smo operacije seznamov, kot so ustvarjanje, poimenovanje, združevanje in brisanje seznama v jeziku R. Seznam R je pomemben koncept in ga ne smete preskočiti.
Upam, da ste se v tem članku naučili o seznamih R in njihovih operacijah.
Preverite tudi:
- R-matrika
- R- Tuples
- R – Matrike