목록 R 프로그래밍 순서가 지정된 개체 컬렉션으로 구성된 일반 개체입니다. 목록은 1차원적인 , 이질적인 데이터 구조.
목록은 다음의 목록일 수 있습니다. 벡터 , 행렬 목록, 문자 목록, 기능 , 등등.
목록은 벡터이지만 이질적인 데이터 요소가 있습니다. R의 목록은 다음을 사용하여 생성됩니다. 목록() 함수 .
R에서는 인덱스 값을 사용하여 R 목록의 요소에 액세스할 수 있습니다. R에서는 목록의 인덱싱이 0 대신 1로 시작합니다.
목록 만들기
R에서 목록을 생성하려면 다음과 같은 함수를 사용해야 합니다. 목록() .
즉, 목록은 다른 객체를 포함하는 일반 벡터입니다. 목록이 어떻게 보이는지 설명하기 위해 여기에 예를 들어 보겠습니다. 우리는 세부적인 직원 목록을 작성하고 싶습니다. 이를 위해 ID, 직원 이름, 직원 수와 같은 속성이 필요합니다.
예:
아르 자형
# 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)> |
>
>산출
[[1]] [1] 1 2 3 4 [[2]] [1] 'Debi' 'Sandeep' 'Subham' 'Shiba' [[3]] [1] 4>
명명 목록 구성 요소
목록 구성 요소에 이름을 지정하면 해당 구성 요소에 더 쉽게 액세스할 수 있습니다.
json 파일을 여는 방법
예:
아르 자형
# Creating a named list> my_named_list <->list>(name =>'Sudheer'>, age = 25, city =>'Delhi'>)> # Printing the named list> print>(my_named_list)> |
>
>산출
$name [1] 'Sudheer' $age [1] 25 $city [1] 'Delhi'>
R 목록 구성 요소에 액세스
두 가지 방법으로 R 목록의 구성 요소에 액세스할 수 있습니다.
1. 이름으로 구성 요소에 액세스합니다.
목록의 모든 구성 요소에는 이름이 지정될 수 있으며 해당 이름을 사용하여 dollar 명령을 사용하여 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)> |
>
>산출
$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. 인덱스별로 구성 요소에 액세스합니다.
인덱스를 사용하여 R 목록의 구성 요소에 액세스할 수도 있습니다.
R 목록의 최상위 구성 요소에 액세스하려면 이중 슬라이싱 연산자를 사용해야 합니다. [[ ]] 이는 두 개의 대괄호이며 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])> |
>
>산출
$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...>
목록의 구성 요소 수정
구성요소에 액세스하고 이를 원하는 구성요소로 대체하여 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)> |
>
>산출
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' ...>
목록 연결
두 개의 R 목록은 연결 기능을 사용하여 연결할 수 있습니다. 따라서 두 목록을 연결하려면 연결 연산자를 사용해야 합니다.
통사론:
목록 = c(목록, 목록1)
목록 = 원래 목록
list1 = 새 목록
예:
여우 또는 늑대
아르 자형
# 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)> |
>
>산출
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...>
목록에 항목 추가
목록 끝에 항목을 추가하려면 add() 함수를 사용할 수 있습니다.
아르 자형
# 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> |
>
>산출
[1] 1 5 6 3 45 [1] 1 5 6 3>
목록의 구성 요소 삭제
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])> |
>
>산출
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...>
병합 목록
모든 목록을 단일 목록에 배치하여 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)> |
>
>
산출:
[[1]] [1] 1 [[2]] [1] 2 [[3]] [1] 3 [[4]] [1] 'Sun' [[5]] [1] 'Mon' [[6]] [1] 'Tue'>
목록을 벡터로 변환
여기서는 R 목록을 벡터로 변환할 것입니다. 이를 위해 먼저 목록을 만든 다음 목록을 벡터로 나열 해제합니다.
새로운 라인 파이썬
아르 자형
# Create lists.> lst <->list>(1:5)> print>(lst)> # Convert the lists to vectors.> vec <->unlist>(lst)> print>(vec)> |
>
>산출
[[1]] [1] 1 2 3 4 5 [1] 1 2 3 4 5>
R 목록을 행렬로
R 프로그래밍에서 행렬() 함수를 사용하여 행렬을 생성해 보겠습니다. 사용될 또 다른 함수는 목록을 벡터로 변환하는 unlist() 함수입니다.
아르 자형
# 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),>'
'>)> |
>
>산출
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,...>
이 기사에서 우리는 다뤘습니다. R의 목록, 우리는 R 언어로 목록 생성, 이름 지정, 병합 및 삭제와 같은 목록 작업을 다루었습니다. R 목록은 중요한 개념이므로 건너뛰어서는 안 됩니다.
이 기사에서 R 목록과 그 작업에 대해 배웠기를 바랍니다.
또한 확인하십시오:
- R-배열
- R- 튜플
- R – 행렬