1 S3 classes
- What is the class of the object returned by the lm() function?
x <- lm(speed ~ dist, cars)
class(x)
## [1] "lm"
- What basic data type is it built upon?
## [1] "list"
- What attributes does the object of the lm class have?
## $names
## [1] "coefficients" "residuals" "effects" "rank"
## [5] "fitted.values" "assign" "qr" "df.residual"
## [9] "xlevels" "call" "terms" "model"
##
## $class
## [1] "lm"
- What is the structure of the lm object?
## List of 12
## $ coefficients : Named num [1:2] 8.284 0.166
## ..- attr(*, "names")= chr [1:2] "(Intercept)" "dist"
## $ residuals : Named num [1:50] -4.62 -5.94 -1.95 -4.93 -2.93 ...
## ..- attr(*, "names")= chr [1:50] "1" "2" "3" "4" ...
## $ effects : Named num [1:50] -108.894 29.866 -0.501 -3.945 -1.797 ...
## ..- attr(*, "names")= chr [1:50] "(Intercept)" "dist" "" "" ...
## $ rank : int 2
## $ fitted.values: Named num [1:50] 8.62 9.94 8.95 11.93 10.93 ...
## ..- attr(*, "names")= chr [1:50] "1" "2" "3" "4" ...
## $ assign : int [1:2] 0 1
## $ qr :List of 5
## ..$ qr : num [1:50, 1:2] -7.071 0.141 0.141 0.141 0.141 ...
## .. ..- attr(*, "dimnames")=List of 2
## .. .. ..$ : chr [1:50] "1" "2" "3" "4" ...
## .. .. ..$ : chr [1:2] "(Intercept)" "dist"
## .. ..- attr(*, "assign")= int [1:2] 0 1
## ..$ qraux: num [1:2] 1.14 1.15
## ..$ pivot: int [1:2] 1 2
## ..$ tol : num 1e-07
## ..$ rank : int 2
## ..- attr(*, "class")= chr "qr"
## $ df.residual : int 48
## $ xlevels : Named list()
## $ call : language lm(formula = speed ~ dist, data = cars)
## $ terms :Classes 'terms', 'formula' language speed ~ dist
## .. ..- attr(*, "variables")= language list(speed, dist)
## .. ..- attr(*, "factors")= int [1:2, 1] 0 1
## .. .. ..- attr(*, "dimnames")=List of 2
## .. .. .. ..$ : chr [1:2] "speed" "dist"
## .. .. .. ..$ : chr "dist"
## .. ..- attr(*, "term.labels")= chr "dist"
## .. ..- attr(*, "order")= int 1
## .. ..- attr(*, "intercept")= int 1
## .. ..- attr(*, "response")= int 1
## .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
## .. ..- attr(*, "predvars")= language list(speed, dist)
## .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
## .. .. ..- attr(*, "names")= chr [1:2] "speed" "dist"
## $ model :'data.frame': 50 obs. of 2 variables:
## ..$ speed: num [1:50] 4 4 7 7 8 9 10 10 10 11 ...
## ..$ dist : num [1:50] 2 10 4 22 16 10 18 26 34 17 ...
## ..- attr(*, "terms")=Classes 'terms', 'formula' language speed ~ dist
## .. .. ..- attr(*, "variables")= language list(speed, dist)
## .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
## .. .. .. ..- attr(*, "dimnames")=List of 2
## .. .. .. .. ..$ : chr [1:2] "speed" "dist"
## .. .. .. .. ..$ : chr "dist"
## .. .. ..- attr(*, "term.labels")= chr "dist"
## .. .. ..- attr(*, "order")= int 1
## .. .. ..- attr(*, "intercept")= int 1
## .. .. ..- attr(*, "response")= int 1
## .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
## .. .. ..- attr(*, "predvars")= language list(speed, dist)
## .. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
## .. .. .. ..- attr(*, "names")= chr [1:2] "speed" "dist"
## - attr(*, "class")= chr "lm"
- Does the lm() class implement own str()?
## List of 12
## $ coefficients : Named num [1:2] 8.284 0.166
## ..- attr(*, "names")= chr [1:2] "(Intercept)" "dist"
## $ residuals : Named num [1:50] -4.62 -5.94 -1.95 -4.93 -2.93 ...
## ..- attr(*, "names")= chr [1:50] "1" "2" "3" "4" ...
## $ effects : Named num [1:50] -108.894 29.866 -0.501 -3.945 -1.797 ...
## ..- attr(*, "names")= chr [1:50] "(Intercept)" "dist" "" "" ...
## $ rank : int 2
## $ fitted.values: Named num [1:50] 8.62 9.94 8.95 11.93 10.93 ...
## ..- attr(*, "names")= chr [1:50] "1" "2" "3" "4" ...
## $ assign : int [1:2] 0 1
## $ qr :List of 5
## ..$ qr : num [1:50, 1:2] -7.071 0.141 0.141 0.141 0.141 ...
## .. ..- attr(*, "dimnames")=List of 2
## .. .. ..$ : chr [1:50] "1" "2" "3" "4" ...
## .. .. ..$ : chr [1:2] "(Intercept)" "dist"
## .. ..- attr(*, "assign")= int [1:2] 0 1
## ..$ qraux: num [1:2] 1.14 1.15
## ..$ pivot: int [1:2] 1 2
## ..$ tol : num 1e-07
## ..$ rank : int 2
## ..- attr(*, "class")= chr "qr"
## $ df.residual : int 48
## $ xlevels : Named list()
## $ call : language lm(formula = speed ~ dist, data = cars)
## $ terms :Classes 'terms', 'formula' language speed ~ dist
## .. ..- attr(*, "variables")= language list(speed, dist)
## .. ..- attr(*, "factors")= int [1:2, 1] 0 1
## .. .. ..- attr(*, "dimnames")=List of 2
## .. .. .. ..$ : chr [1:2] "speed" "dist"
## .. .. .. ..$ : chr "dist"
## .. ..- attr(*, "term.labels")= chr "dist"
## .. ..- attr(*, "order")= int 1
## .. ..- attr(*, "intercept")= int 1
## .. ..- attr(*, "response")= int 1
## .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
## .. ..- attr(*, "predvars")= language list(speed, dist)
## .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
## .. .. ..- attr(*, "names")= chr [1:2] "speed" "dist"
## $ model :'data.frame': 50 obs. of 2 variables:
## ..$ speed: num [1:50] 4 4 7 7 8 9 10 10 10 11 ...
## ..$ dist : num [1:50] 2 10 4 22 16 10 18 26 34 17 ...
## ..- attr(*, "terms")=Classes 'terms', 'formula' language speed ~ dist
## .. .. ..- attr(*, "variables")= language list(speed, dist)
## .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
## .. .. .. ..- attr(*, "dimnames")=List of 2
## .. .. .. .. ..$ : chr [1:2] "speed" "dist"
## .. .. .. .. ..$ : chr "dist"
## .. .. ..- attr(*, "term.labels")= chr "dist"
## .. .. ..- attr(*, "order")= int 1
## .. .. ..- attr(*, "intercept")= int 1
## .. .. ..- attr(*, "response")= int 1
## .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
## .. .. ..- attr(*, "predvars")= language list(speed, dist)
## .. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
## .. .. .. ..- attr(*, "names")= chr [1:2] "speed" "dist"
- What is the class of a tibble?
## -- Attaching packages --------------------------------------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 2.2.1 v purrr 0.2.5
## v tibble 1.4.2 v dplyr 0.7.5
## v tidyr 0.8.1 v stringr 1.3.1
## v readr 1.1.1 v forcats 0.3.0
## -- Conflicts ------------------------------------------------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
df <- as_tibble(mtcars)
class(df)
## [1] "tbl_df" "tbl" "data.frame"
- What is the underlying data type?
## [1] "list"
- Is the str function used by tibbles the standard str function?
## Classes 'tbl_df', 'tbl' and 'data.frame': 32 obs. of 11 variables:
## $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
## $ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
## $ disp: num 160 160 108 258 360 ...
## $ hp : num 110 110 93 110 175 105 245 62 95 123 ...
## $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
## $ wt : num 2.62 2.88 2.32 3.21 3.44 ...
## $ qsec: num 16.5 17 18.6 19.4 17 ...
## $ vs : num 0 0 1 1 0 1 0 1 1 1 ...
## $ am : num 1 1 1 0 0 0 0 0 0 0 ...
## $ gear: num 4 4 4 3 3 3 3 4 4 4 ...
## $ carb: num 4 4 1 1 2 1 4 2 2 4 ...
## List of 11
## $ mpg : num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
## $ cyl : num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
## $ disp: num [1:32] 160 160 108 258 360 ...
## $ hp : num [1:32] 110 110 93 110 175 105 245 62 95 123 ...
## $ drat: num [1:32] 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
## $ wt : num [1:32] 2.62 2.88 2.32 3.21 3.44 ...
## $ qsec: num [1:32] 16.5 17 18.6 19.4 17 ...
## $ vs : num [1:32] 0 0 1 1 0 1 0 1 1 1 ...
## $ am : num [1:32] 1 1 1 0 0 0 0 0 0 0 ...
## $ gear: num [1:32] 4 4 4 3 3 3 3 4 4 4 ...
## $ carb: num [1:32] 4 4 1 1 2 1 4 2 2 4 ...
## - attr(*, "row.names")= chr [1:32] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive" ...