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
|
> # Vector with numerics from 1 up to 10
> my_vector <- 1:10
>
> # Matrix with numerics from 1 up to 9
> my_matrix <- matrix(1:9, ncol = 3)
>
> # First 10 elements of the built-in data frame mtcars
> my_df <- mtcars[1:10,]
>
> # Construct list with these different elements:
> my_list <- list(my_vector, my_matrix, my_df)
> my_list
[[1]]
[1] 1 2 3 4 5 6 7 8 9 10
[[2]]
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
[[3]]
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
| cs |
리스트는 벡터, 매트릭스, 데이터 프레임, 리스트 등 모든 것을 포함할 수 있다.
2. 리스트의 성질(2)
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
|
> # Vector with numerics from 1 up to 10
> my_vector <- 1:10
>
> # Matrix with numerics from 1 up to 9
> my_matrix <- matrix(1:9, ncol = 3)
>
> # First 10 elements of the built-in data frame mtcars
> my_df <- mtcars[1:10,]
>
> # Adapt list() call to give the components names
> my_list <- list(vec = my_vector, mat = my_matrix, df = my_df)
>
> # Print out my_list
> my_list
$vec
[1] 1 2 3 4 5 6 7 8 9 10
$mat
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
$df
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
| cs |
리스트는 모든 요소를 포함할 수 있다. 리스트가 포함하는 요소의 성질을 기억하기 힘들기 때문에 요소에 이름을 부여하여 효율적으로 리스트를 관리할 수 있다.
3. 리스트의 요소 선택하기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
> # shining_list is already pre-loaded in the workspace
>
> # Print out the vector representing the actors
> shining_list[["actors"]]
[1] "Jack Nicholson" "Shelley Duvall" "Danny Lloyd" "Scatman Crothers"
[5] "Barry Nelson"
>
> shining_list$actors
[1] "Jack Nicholson" "Shelley Duvall" "Danny Lloyd" "Scatman Crothers"
[5] "Barry Nelson"
>
> # Print the second element of the vector representing the actors
> shining_list[["actors"]][2]
[1] "Shelley Duvall"
| cs |
두 개의 대괄호 사이에 "열 이름"을 입력하거나([["열 이름"]]),
$ 뒤에 열 이름을 입력한다($열 이름).
대괄호를 사용할 경우 따옴표를 사용해야하고,
$을 사용할 경우 따옴표로 사용하지 않는다.
4. 리스트에 요소 추가하기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
> # shining_list, the list containing movie name, actors and reviews, is pre-loaded in the workspace
>
> # We forgot something; add the year to shining_list
> shining_list_full <- c(shining_list, year = 1980)
> # ext_list <- c(my_list, 추가하려는 요소의 이름 = 추가하려는 요소)
>
> # Have a look at shining_list_full
> str(shining_list_full)
List of 4
$ moviename: chr "The Shining"
$ actors : chr [1:5] "Jack Nicholson" "Shelley Duvall" "Danny Lloyd" "Scatman Crothers" ...
$ reviews :'data.frame': 3 obs. of 3 variables:
..$ scores : num [1:3] 4.5 4 5
..$ sources : Factor w/ 3 levels "IMDb1","IMDb2",..: 1 2 3
..$ comments: Factor w/ 3 levels "A masterpiece of psychological horror",..: 3 2 1
$ year : num 1980
| cs |
댓글 없음:
댓글 쓰기