logo

R – saraksti

Iekšā saraksts R programmēšana ir vispārīgs objekts, kas sastāv no sakārtotas objektu kolekcijas. Saraksti ir viendimensionāls , neviendabīgs datu struktūras.

Saraksts var būt saraksts ar vektori , matricu saraksts, rakstzīmju saraksts, saraksts ar funkcijas , un tā tālāk.



Saraksts ir vektors, bet ar neviendabīgiem datu elementiem. Saraksts R tiek izveidots, izmantojot list() funkcija .

R ļauj piekļūt R saraksta elementiem, izmantojot indeksa vērtību. R valodā saraksta indeksēšana sākas ar 1, nevis 0.

Saraksta izveide

Lai izveidotu sarakstu R, jums jāizmanto funkcija ar nosaukumu saraksts () .



Citiem vārdiem sakot, saraksts ir vispārīgs vektors, kas satur citus objektus. Lai ilustrētu, kā izskatās saraksts, mēs ņemam piemēru. Mēs vēlamies izveidot darbinieku sarakstu ar detaļām. Tāpēc mēs vēlamies tādus atribūtus kā ID, darbinieka vārds un darbinieku skaits.

Piemērs:

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)>

>

>

Izvade

[[1]] [1] 1 2 3 4 [[2]] [1] 'Debi' 'Sandeep' 'Subham' 'Shiba' [[3]] [1] 4>

Nosaukšanas saraksta komponenti

Nosaukšanas saraksta komponenti atvieglo tiem piekļuvi.

kārtošana sarakstā java

Piemērs:

R




# Creating a named list> my_named_list <->list>(name =>'Sudheer'>, age = 25, city =>'Delhi'>)> # Printing the named list> print>(my_named_list)>

>

>

Izvade

$name [1] 'Sudheer' $age [1] 25 $city [1] 'Delhi'>

Piekļuve R saraksta komponentiem

Mēs varam piekļūt R saraksta komponentiem divos veidos.

1. Piekļūstiet komponentiem pēc nosaukumiem:

Visas saraksta sastāvdaļas var nosaukt, un mēs varam izmantot šos nosaukumus, lai piekļūtu R saraksta komponentiem, izmantojot dolāru komandu.

Piemērs:

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)>

>

>

Izvade

$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. Piekļūstiet komponentiem pēc indeksiem:

Mēs varam piekļūt arī R saraksta komponentiem, izmantojot indeksus.

Lai piekļūtu R saraksta augstākā līmeņa komponentiem, mums ir jāizmanto dubultās sagriešanas operators [[]] kas ir divas kvadrātiekavas, un, ja mēs vēlamies piekļūt R saraksta apakšējā vai iekšējā līmeņa sastāvdaļām, mums ir jāizmanto cita kvadrātiekava [ ] kopā ar dubultās griešanas operatoru [[]] .

Piemērs:

R


Katrīna kaif



# 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])>

>

>

Izvade

$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...>

Saraksta komponentu modificēšana

R sarakstu var arī mainīt, piekļūstot komponentiem un aizstājot tos ar tiem, kurus vēlaties.

Piemērs:

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)>

>

>

Izvade

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' ...>

Sarakstu savienošana

Divus R sarakstus var savienot, izmantojot savienošanas funkciju. Tātad, ja mēs vēlamies savienot divus sarakstus, mums ir jāizmanto savienošanas operators.

Sintakse:

saraksts = c(saraksts, saraksts1)
saraksts = sākotnējais saraksts
saraksts1 = jaunais saraksts

Piemērs:

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)>

>

>

Izvade

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...>

Vienuma pievienošana sarakstam

Lai saraksta beigās pievienotu vienumu, mēs varam izmantot append() funkciju.

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>

>

>

Izvade

[1] 1 5 6 3 45 [1] 1 5 6 3>

Saraksta komponentu dzēšana

Lai dzēstu R saraksta komponentus, mums vispirms ir jāpiekļūst šiem komponentiem un pēc tam pirms šiem komponentiem jāievieto negatīva zīme. Tas norāda, ka mums bija jāizdzēš šis komponents.

Piemērs:

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])>

>

>

Izvade

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...>

Apvienojot sarakstu

Mēs varam apvienot R sarakstu, ievietojot visus sarakstus vienā sarakstā.

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)>

>

>

Izvade:

[[1]] [1] 1 [[2]] [1] 2 [[3]] [1] 3 [[4]] [1] 'Sun' [[5]] [1] 'Mon' [[6]] [1] 'Tue'>

Saraksta pārveidošana par vektoru

Šeit mēs pārveidosim R sarakstu par vektoru, šim nolūkam mēs vispirms izveidosim sarakstu un pēc tam sarakstu izņemsim vektorā.

R




# Create lists.> lst <->list>(1:5)> print>(lst)> # Convert the lists to vectors.> vec <->unlist>(lst)> print>(vec)>

>

>

Izvade

[[1]] [1] 1 2 3 4 5 [1] 1 2 3 4 5>

R Saraksts uz matricu

Mēs izveidosim matricas, izmantojot R programmēšanas funkciju matrix(). Vēl viena funkcija, kas tiks izmantota, ir unlist() funkcija, lai pārvērstu sarakstus vektoros.

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),>' '>)>

>

>

Izvade

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,...>

Šajā rakstā mēs esam aplūkojuši Saraksti R, mēs esam apskatījuši sarakstu darbības, piemēram, saraksta izveidi, nosaukumu piešķiršanu, apvienošanu un dzēšanu R valodā. R saraksts ir svarīgs jēdziens, un to nevajadzētu izlaist.

Cerams, ka šajā rakstā uzzinājāt par R sarakstiem un to darbībām.

kas ir ziemas guļas režīms

Pārbaudiet arī: