R – Seznamy

Seznam v R programování je obecný objekt sestávající z uspořádané kolekce objektů. Seznamy jsou jednorozměrný , heterogenní datové struktury.

Seznam může být seznamem vektory , seznam matrik, seznam znaků, seznam funkcí , a tak dále.

Seznam je vektor, ale s heterogenními datovými prvky. Seznam v R je vytvořen s použitím funkce list(). .

R umožňuje přístup k prvkům seznamu R s použitím hodnoty indexu. V R začíná indexování seznamu 1 místo 0.

Vytvoření seznamu

Chcete-li vytvořit seznam v R, musíte použít funkci s názvem seznam() .

Jinými slovy, seznam je obecný vektor obsahující další objekty. Abychom ilustrovali, jak seznam vypadá, uvádíme příklad. Chceme sestavit seznam zaměstnanců s podrobnostmi. Za tímto účelem potřebujeme atributy, jako je ID, jméno zaměstnance a počet zaměstnanců.

Příklad:

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

Výstup

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

Komponenty seznamu názvů

Komponenty jmenného seznamu usnadňují přístup k nim.

Příklad:

R




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

Výstup

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

Přístup ke komponentám seznamu R

Ke komponentám seznamu R můžeme přistupovat dvěma způsoby.

1. Přístup ke komponentám podle jmen:

Všechny komponenty seznamu lze pojmenovat a tato jména můžeme použít pro přístup ke komponentám seznamu R pomocí příkazu dollar.

Příklad:

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

Výstup

$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. Přístup ke komponentám podle indexů:

Ke komponentám seznamu R můžeme přistupovat také pomocí indexů.

Pro přístup ke komponentám nejvyšší úrovně seznamu R musíme použít operátor dvojitého dělení [[ ]] což jsou dvě hranaté závorky a pokud chceme přistupovat ke komponentám na nižší nebo vnitřní úrovni seznamu R, musíme použít jinou hranatou závorku [ ] spolu s operátorem dvojitého krájení [[ ]] .

Příklad:

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

Výstup

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

Úprava součástí seznamu

Seznam R lze také upravit přístupem ke komponentám a jejich nahrazením těmi, které chcete.

Příklad:

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

Výstup

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

Zřetězení seznamů

Dva seznamy R lze zřetězit pomocí funkce zřetězení. Když tedy chceme zřetězit dva seznamy, musíme použít operátor zřetězení.

Syntax:

seznam = c(seznam, seznam1)
seznam = původní seznam
seznam1 = nový seznam

Příklad:

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

Výstup

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

Přidání položky do seznamu

Pro přidání položky na konec seznamu můžeme použít funkci 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>

Výstup

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

Odstranění součástí seznamu

Abychom odstranili komponenty seznamu R, musíme nejprve k těmto komponentám přistupovat a poté před tyto komponenty vložit záporné znaménko. Znamená to, že jsme museli tuto komponentu odstranit.

Příklad:

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

Výstup

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

Slučovací seznam

Seznam R můžeme sloučit umístěním všech seznamů do jednoho seznamu.

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

Výstup:

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

Převod seznamu na vektor

Zde převedeme seznam R na vektor, k tomu nejprve vytvoříme seznam a poté seznam zrušíme do vektoru.

R




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

Výstup

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

R Seznam do matice

Vytvoříme matice pomocí funkce matrix() v programování R. Další funkcí, která bude použita, je funkce unlist() pro převod seznamů na vektor.

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

Výstup

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 tomto článku jsme se zabývali seznamy v R, probrali jsme operace se seznamy, jako je vytváření, pojmenování, slučování a mazání seznamu v jazyce R. R seznam je důležitý koncept a neměl by být přeskočen.

Doufám, že jste se v tomto článku dozvěděli o seznamech R a jejich operacích.

Zkontrolujte také:



Mohlo By Se Vám Líbit