logo

R – sąrašai

Įtrauktas sąrašas R programavimas yra bendras objektas, susidedantis iš sutvarkytos objektų rinkinio. Sąrašai yra vienmatis , nevienalytis duomenų struktūros.

Sąrašas gali būti sąrašas vektoriai , matricų sąrašas, simbolių sąrašas, sąrašas funkcijas , ir taip toliau.



Sąrašas yra vektorius, bet su nevienalyčiais duomenų elementais. Sąrašas R sukuriamas naudojant list() funkcija .

R leidžia pasiekti R sąrašo elementus naudojant indekso reikšmę. R kalbant, sąrašo indeksavimas prasideda nuo 1, o ne nuo 0.

Sąrašo kūrimas

Norėdami sukurti sąrašą R, turite naudoti funkciją, vadinamą sąrašas () .



Kitaip tariant, sąrašas yra bendras vektorius, kuriame yra kitų objektų. Norėdami parodyti, kaip atrodo sąrašas, pateikiame pavyzdį. Norime sudaryti darbuotojų sąrašą su informacija. Todėl norime tokių atributų kaip ID, darbuotojo vardas ir pavardė ir darbuotojų skaičius.

Pavyzdys:

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

>

>

Išvestis

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

Vardų sąrašo komponentai

Pavadinimų sąrašo komponentai palengvina jų prieigą.

darbuotojų atrankos komisijos reikšmė

Pavyzdys:

R




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

>

>

Išvestis

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

Prieiga prie R sąrašo komponentų

R sąrašo komponentus galime pasiekti dviem būdais.

1. Pasiekite komponentus pagal pavadinimus:

Visi sąrašo komponentai gali būti pavadinti ir mes galime naudoti tuos pavadinimus norėdami pasiekti R sąrašo komponentus naudodami komandą doleris.

Pavyzdys:

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

>

>

Išvestis

$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. Pasiekite komponentus pagal indeksus:

R sąrašo komponentus taip pat galime pasiekti naudodami indeksus.

Norėdami pasiekti aukščiausio lygio R sąrašo komponentus, turime naudoti dvigubo pjaustymo operatorių [[ ]] kuris yra du laužtiniai skliaustai ir jei norime pasiekti žemesnio arba vidinio lygio R sąrašo komponentus, turime naudoti kitą laužtinį skliaustą [ ] kartu su dvigubo pjaustymo operatoriumi [[]] .

Pavyzdys:

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

>

nemokama vs nemokama

>

Išvestis

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

Sąrašo komponentų keitimas

R sąrašą taip pat galima keisti pasiekus komponentus ir pakeičiant juos norimais.

Pavyzdys:

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

>

>

Išvestis

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

Sąrašų sujungimas

Du R sąrašai gali būti sujungti naudojant sujungimo funkciją. Taigi, kai norime sujungti du sąrašus, turime naudoti sujungimo operatorių.

Sintaksė:

sąrašas = c(sąrašas, sąrašas1)
sąrašas = originalus sąrašas
list1 = naujas sąrašas

Pavyzdys:

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

>

>

Išvestis

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

Prekės įtraukimas į sąrašą

Norėdami įtraukti elementą į sąrašo pabaigą, galime naudoti funkciją 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>

>

>

Išvestis

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

Sąrašo komponentų ištrynimas

Norėdami ištrinti R sąrašo komponentus, pirmiausia turime pasiekti tuos komponentus ir prieš tuos komponentus įterpti neigiamą ženklą. Tai rodo, kad turėjome ištrinti tą komponentą.

Pavyzdys:

R




css centruoja vaizdą

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

>

>

Išvestis

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

Sujungiamas sąrašas

Mes galime sujungti R sąrašą sudėję visus sąrašus į vieną sąrašą.

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

>

>

Išvestis:

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

Sąrašo konvertavimas į vektorių

Čia mes ketiname konvertuoti R sąrašą į vektorių, tam pirmiausia sukursime sąrašą, o tada pašalinsime sąrašą į vektorių.

R




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

>

>

Išvestis

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

R Sąrašas į matricą

Matricas kursime naudodami matricos() funkciją R programavime. Kita funkcija, kuri bus naudojama, yra funkcija unlist(), kuri konvertuoja sąrašus į vektorių.

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

>

>

Išvestis

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

Šiame straipsnyje mes apžvelgėme Sąrašai R, apėmėme sąrašo operacijas, tokias kaip sąrašo kūrimas, pavadinimas, sujungimas ir ištrynimas R kalba. R sąrašas yra svarbi sąvoka ir jo nereikėtų praleisti.

Tikimės, kad šiame straipsnyje sužinojote apie R sąrašus ir jo operacijas.

Taip pat patikrinkite: