logo

R – Listy

Lista w Programowanie R jest obiektem ogólnym składającym się z uporządkowanej kolekcji obiektów. Listy są jednowymiarowy , heterogeniczny struktury danych.

Lista może być listą wektory , lista macierzy, lista znaków, lista Funkcje , i tak dalej.



Lista jest wektorem, ale zawiera heterogeniczne elementy danych. Listę w R tworzy się za pomocą metody funkcja list(). .

R umożliwia dostęp do elementów listy R za pomocą wartości indeksu. W R indeksowanie listy zaczyna się od 1 zamiast od 0.

Tworzenie listy

Aby utworzyć listę w R, musisz użyć funkcji o nazwie lista() .



Innymi słowy, lista jest ogólnym wektorem zawierającym inne obiekty. Aby zilustrować wygląd listy, posłużymy się przykładem. Chcemy zbudować listę pracowników ze szczegółami. W tym celu potrzebujemy atrybutów, takich jak identyfikator, imię i nazwisko pracownika i liczba pracowników.

Przykład:

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

>

>

Wyjście

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

Składniki listy nazw

Komponenty list nazewniczych ułatwiają dostęp do nich.

znajdź ciąg znaków C++

Przykład:

R




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

>

>

Wyjście

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

Dostęp do komponentów listy R

Dostęp do komponentów listy R możemy uzyskać na dwa sposoby.

1. Uzyskaj dostęp do komponentów według nazw:

Wszystkim składnikom listy można nadać nazwy i za pomocą tych nazw możemy uzyskać dostęp do składników listy R za pomocą polecenia dolara.

Przykład:

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

>

>

Wyjście

$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. Dostęp do komponentów według indeksów:

Dostęp do komponentów listy R możemy uzyskać także za pomocą indeksów.

Aby uzyskać dostęp do komponentów najwyższego poziomu listy R, musimy użyć operatora podwójnego krojenia [[ ]] czyli dwa nawiasy kwadratowe i jeśli chcemy uzyskać dostęp do komponentów niższego lub wewnętrznego poziomu listy R, musimy użyć innego nawiasu kwadratowego [ ] wraz z operatorem podwójnego krojenia [[ ]] .

Przykład:

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

>

>

Wyjście

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

Modyfikowanie składników listy

Listę R można również modyfikować, uzyskując dostęp do komponentów i zastępując je tymi, które chcesz.

zmień nazwę folderu Linux

Przykład:

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

>

>

Wyjście

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

Łączenie list

Dwie listy R można połączyć za pomocą funkcji konkatenacji. Jeśli więc chcemy połączyć dwie listy, musimy użyć operatora łączenia.

Składnia:

lista = c(lista, lista1)
lista = oryginalna lista
lista1 = nowa lista

Przykład:

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

>

>

Wyjście

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

Dodawanie pozycji do listy

Aby dodać pozycję na koniec listy, możemy skorzystać z funkcji 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>

>

>

Wyjście

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

Usuwanie składników listy

Aby usunąć komponenty listy R, najpierw musimy uzyskać dostęp do tych komponentów, a następnie wstawić znak minus przed tymi komponentami. Wskazuje, że musieliśmy usunąć ten komponent.

Przykład:

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

>

ciąg Java do tablicy

>

Wyjście

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

Łączenie listy

Możemy połączyć listę R, umieszczając wszystkie listy w jednej liście.

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

>

>

Wyjście:

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

Konwersja listy na wektor

Tutaj skonwertujemy listę R na wektor, w tym celu najpierw utworzymy listę, a następnie przeniesiemy ją do wektora.

R




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

>

>

Wyjście

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

R Lista do macierzy

Macierze będziemy tworzyć za pomocą funkcji matrix() w programowaniu w R. Inną funkcją, która zostanie użyta, jest funkcja unlist() służąca do konwersji list na wektor.

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

>

>

Wyjście

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

W tym artykule omówiliśmy Listy w R, omówiliśmy operacje na listach, takie jak tworzenie, nazywanie, łączenie i usuwanie list w języku R. Lista R to ważna koncepcja i nie należy jej pomijać.

Mam nadzieję, że z tego artykułu dowiedziałeś się o listach R i ich operacjach.

Sprawdź także: