[사용 함수]
함수명 | 설명 |
list() | 리스트 생성 |
str() | 데이터 구조(structure) 확인 |
length() | Key 개수 반환 |
NROW() | Key 값들만 하나의 칼럼으로 구성된 형태로 인식 |
NCOL() | Key 값들만 하나의 칼럼으로 구성된 형태로 인식 |
nrow() | 직사각형이 아니기 때문에 null |
ncol () | 직사각형이 아니기 때문에 null |
[리스트 (List)]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | > # list > # 같은 것을 의미하는 것들 알아 두기 > list1 <- list(name=c('James Seo',"harim"), address='Seoul', tel='010-8706-4712', pay=500) ; list1 $name [1] "James Seo" "harim" $address [1] "Seoul" $tel [1] "010-8706-4712" $pay [1] 500 > str(list1) # 4개의 sublist로 이루어진 리스트 List of 4 $ name : chr [1:2] "James Seo" "harim" $ address: chr "Seoul" $ tel : chr "010-8706-4712" $ pay : num 500 | cs |
[리스트 데이터 제어]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | > # 리스트 데이터 제어 > > # 벡터 반환 > list1$name [1] "James Seo" "harim" > list1[[1]] [1] "James Seo" "harim" > list1[["name"]] [1] "James Seo" "harim" > list1[[c(1,3)]] # 1번째 키의 값과 3번째 키의 값을 보려고 하지만 오류 Error in list1[[c(1, 3)]] : subscript out of bounds > > # 서브 리스트 반환 > list1[1] # 첫번째 키, 밸류 모두 반환 (서브리스트) $name [1] "James Seo" "harim" > list1["name"] # 키 이름으로 찾기 가능 $name [1] "James Seo" "harim" > list1[c(1,3)] # 1번째, 3번째 키 $name [1] "James Seo" "harim" $tel [1] "010-8706-4712" > > # 새로운 키와 밸류 추가 > list1$birth <- as.Date('1975-10-23') #생일을 추가 > list1$no.children <- 3 #자녀수 추가 > list1$child.ages <- c(10, 7, 5) #각 자녀에 대한 나이 추가 > list1 $name [1] "James Seo" "harim" $address [1] "Seoul" $tel [1] "010-8706-4712" $pay [1] 500 $birth [1] "1975-10-23" $no.children [1] 3 $child.ages [1] 10 7 5 > > list1$birth <- NULL ; list1 #birth 키/값 삭제 $name [1] "James Seo" "harim" $address [1] "Seoul" $tel [1] "010-8706-4712" $pay [1] 500 $no.children [1] 3 $child.ages [1] 10 7 5 > > list1[c(1,2,3,5,4,6)] # Key 순서 바꾸기 $name [1] "James Seo" "harim" $address [1] "Seoul" $tel [1] "010-8706-4712" $no.children [1] 3 $pay [1] 500 $child.ages [1] 10 7 5 > list1[-c(1,3)] # 1,3 번째 Key 빼고 나머지 sublist $address [1] "Seoul" $pay [1] 500 $no.children [1] 3 $child.ages [1] 10 7 5 > list1[c(-1,-3)] # 상동동 $address [1] "Seoul" $pay [1] 500 $no.children [1] 3 $child.ages [1] 10 7 5 > > list1[6][1] # Key들 중 6번째 Key 1개 반환 $child.ages [1] 10 7 5 > list1[6][2] # 오류, 6번째 Key에 대해 2번째는 없기 때문 $<NA> NULL > list1[[6]][1] # 6번째 Key의 Value에서 1번째 값만 가져와라 [1] 10 > > # length(), NROW, NCOL, nrow, ncol 함수 > length(list1) # Key 개수 반환 [1] 6 > > NROW(list1) # 하나의 칼럼으로 구성된 형태로 인식 [1] 6 > NCOL(list1) # 하나의 칼럼으로 구성된 형태로 인식 [1] 1 > > nrow(list1) # 직사각형이 아니기 때문에 null NULL > ncol(list1) # 직사각형이 아니기 때문에 null NULL | cs |
출처 - 경영프로그래밍 강의 자료 (양성병 교수님)
'IT,인터넷 관련 학습 > R언어 학습' 카테고리의 다른 글
[R] 데이터 입력 및 출력 (0) | 2019.05.12 |
---|---|
[R] 데이타프레임 (data.frame) (0) | 2019.05.11 |
[R] 배열 (Array) (0) | 2019.05.11 |
[R] 행렬 (Matrix) (0) | 2019.05.11 |
[R] 벡터 (Vector) (0) | 2019.05.11 |