R fundamentals: In-class Exercise 2

2020-Spring [Data Management] Instructor: SHEU, Ching-Fan

CHIU, Ming-Tzu

2020-03-29

The notation, women{datasets}, indicates that a data object by the name women is in the datasets package. This package is preloaded when R is invoked. Explain the difference between c(women) and c(as.matrix(women)) using the women{datasets}.

datasets::women
#>    height weight
#> 1      58    115
#> 2      59    117
#> 3      60    120
#> 4      61    123
#> 5      62    126
#> 6      63    129
#> 7      64    132
#> 8      65    135
#> 9      66    139
#> 10     67    142
#> 11     68    146
#> 12     69    150
#> 13     70    154
#> 14     71    159
#> 15     72    164
str(women)
#> 'data.frame':    15 obs. of  2 variables:
#>  $ height: num  58 59 60 61 62 63 64 65 66 67 ...
#>  $ weight: num  115 117 120 123 126 129 132 135 139 142 ...

c(women)

c(women)
#> $height
#>  [1] 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#> 
#> $weight
#>  [1] 115 117 120 123 126 129 132 135 139 142 146 150 154 159 164

c(women)是將women{datasets}視為dataframe,並把當中的列(column)資料(為height和weight二列)各自挑出來呈現。

c(as.matrix(women))

c(as.matrix(women))
#>  [1]  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72 115 117 120 123
#> [20] 126 129 132 135 139 142 146 150 154 159 164

c(as.matrix(women))則是將women{datasets}看作是一15*2的矩陣(matrix),所挑出來的資料為此矩陣中各個元素,按照順序排列呈現。