Chapter 7 Base R Practice Problems & Quiz

10월 8일 목요일, 202AIE17 송채은

library(ggplot2)
library(psych)
head(bfi)
##       A1 A2 A3 A4 A5 C1 C2 C3 C4 C5 E1 E2 E3 E4 E5 N1 N2 N3 N4 N5 O1 O2 O3 O4 O5 gender education age
## 61617  2  4  3  4  4  2  3  3  4  4  3  3  3  4  4  3  4  2  2  3  3  6  3  4  3      1        NA  16
## 61618  2  4  5  2  5  5  4  4  3  4  1  1  6  4  3  3  3  3  5  5  4  2  4  3  3      2        NA  18
## 61620  5  4  5  4  4  4  5  4  2  5  2  4  4  4  5  4  5  4  2  3  4  2  5  5  2      2        NA  17
## 61621  4  4  6  5  5  4  4  3  5  5  5  3  4  4  4  2  5  2  4  1  3  3  4  3  5      2        NA  17
## 61622  2  3  3  4  5  4  4  5  3  2  2  2  5  4  5  2  3  4  4  3  3  3  4  3  3      1        NA  17
## 61623  6  6  5  6  5  6  6  6  1  3  2  1  6  5  6  3  5  2  2  3  4  3  5  6  1      2         3  21
gender <- bfi$gender
education <- bfi$education
age <- bfi$age
E3 <- bfi$E3
typeof(gender)
## [1] "integer"
typeof(education)
## [1] "integer"
typeof(E3)
## [1] "integer"
typeof(E3)
## [1] "integer"
length(gender)
## [1] 2800
sum(is.na(gender))
## [1] 0
table(gender)
## gender
##    1    2 
##  919 1881
sum(gender == 2) / length(gender)
## [1] 0.6717857
1881/2800
## [1] 0.6717857
round(1881/2800, digits = 2)
## [1] 0.67

dddd

sum(E3[age<26], na.rm = TRUE)
## [1] 5481
sum(E3[gender==1], na.rm = TRUE)
## [1] 3553
sum(E3[gender==2], na.rm = TRUE)
## [1] 7549

여기서부터야!!

E3 <- bfi$E3
p1 <- subset(E3, gender == 1 & age < 26)
p2 <- subset(E3, gender == 2 & age < 26)
mean(p1, na.rm = TRUE) - mean(p2, na.rm = TRUE)
## [1] -0.07071837
sum(bfi$gender == 1)
## [1] 919
sum(gender[age==16 & education == 1], na.rm = TRUE)
## [1] 18
list(gender[age==16 & education == 1], na.rm = TRUE)
## [[1]]
##  [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA  1  1 NA  2 NA  1 NA  2  1 NA NA  1 NA NA  2  1  2 NA  2 NA
## [43] NA  2 NA NA NA NA NA NA NA NA NA NA NA NA
## 
## $na.rm
## [1] TRUE
p3 <- subset(bfi, gender == 1 & age == 16 & education == 1)
count(p3)
##   n
## 1 6
p4 <- subset(bfi, gender == 2 & age == 42)
count(p4)
##    n
## 1 21
library(tidyverse)
bfi
##       A1 A2 A3 A4 A5 C1 C2 C3 C4 C5 E1 E2 E3 E4 E5 N1 N2 N3 N4 N5 O1 O2 O3 O4 O5 gender education age
## 61617  2  4  3  4  4  2  3  3  4  4  3  3  3  4  4  3  4  2  2  3  3  6  3  4  3      1        NA  16
## 61618  2  4  5  2  5  5  4  4  3  4  1  1  6  4  3  3  3  3  5  5  4  2  4  3  3      2        NA  18
## 61620  5  4  5  4  4  4  5  4  2  5  2  4  4  4  5  4  5  4  2  3  4  2  5  5  2      2        NA  17
## 61621  4  4  6  5  5  4  4  3  5  5  5  3  4  4  4  2  5  2  4  1  3  3  4  3  5      2        NA  17
## 61622  2  3  3  4  5  4  4  5  3  2  2  2  5  4  5  2  3  4  4  3  3  3  4  3  3      1        NA  17
## 61623  6  6  5  6  5  6  6  6  1  3  2  1  6  5  6  3  5  2  2  3  4  3  5  6  1      2         3  21
## 61624  2  5  5  3  5  5  4  4  2  3  4  3  4  5  5  1  2  2  1  1  5  2  5  6  1      1        NA  18
## 61629  4  3  1  5  1  3  2  4  2  4  3  6  4  2  1  6  3  2  6  4  3  2  4  5  3      1         2  19
## 61630  4  3  6  3  3  6  6  3  4  5  5  3 NA  4  3  5  5  2  3  3  6  6  6  6  1      1         1  19
## 61633  2  5  6  6  5  6  5  6  2  1  2  2  4  5  5  5  5  5  2  4  5  1  5  5  2      2        NA  17
## 61634  4  4  5  6  5  4  3  5  3  2  1  3  2  5  4  3  3  4  2  3  5  3  5  6  3      1         1  21
## 61636  2  5  5  5  5  5  4  5  4  5  3  3  4  5  4  4  5  3  2 NA  4  6  4  5  4      1        NA  16
## 61637  5  5  5  6  4  5  4  3  2  2  3  3  3  2  4  1  2  2  2  2  4  2  4  5  2      2        NA  16
## 61639  5  5  5  6  6  4  4  4  2  1  2  2  4  6  5  1  1  1  2  1  5  3  4  4  4      1        NA  16
## 61640  4  5  2  2  1  5  5  5  2  2  3  4  3  6  5  2  4  2  2  3  5  2  5  5  5      1         1  17
## 61643  4  3  6  6  3  5  5  5  3  5  1  1  6  6  4  4  5  4  5  5  6  6  6  3  2      1        NA  17
## 61650  4  6  6  2  5  4  4  4  4  4  1  2  5  5  5  4  4  4  4  5  5  1  5  6  3      2        NA  17
## 61651  5  5  5  4  5  5  5  5  4  3  2  2  4  6  6  6  5  5  4  4  5  1  4  5  4      1        NA  17
## 61653  4  4  5  4  3  5  4  5  4  6  1  2  4  5  5  5  6  5  5  2  4  2  2  4  2      2        NA  16
## 61654  4  4  6  5  5  1  1  1  5  6  1  1  4  5  6  5  5  5  1  1  4  1  5  3  2      2        NA  17
## 61656  5  4  2  1  2  4  6  5  5  4  3  3  5  5  4  1  3  3  2  1  6  1  3  2  4      1        NA  17
## 61659  1  6  6  1  5  5  4  4  2  3  1  2  4  3  4  2  5  5  4  6  5  1  6  6  2      2        NA  17
## 61661  1  5  6  5  6  4  3  2  4  5  2  1  2  5  2  2  2  2  2  2  6  1  5  5  2      1         5  68
## 61664  2  6  5  6  5  3  5  6  3  6  2  2  4  6  6  4  4  4  6  6  6  1  5  6  1      2         2  27
## 61667  4  5  5  6  5  5  5  4  1  1  3  2  5  5  6  2  3  3  1  1  6  2  5  6  2      1         1  18
## 61668  1  6  6  1  6  5  2  5  1  1  1  1  6  6  6  2  3  1  2  1  6  4  5  5  3      2         3  20
## 61669  2  4  4  4  3  6  5  6  1  1  2  4  4  2  6  3  3  5  3  2  5  2  6  6  1      2         5  51
## 61670  2  5  6  6  6  4  5  4  3  4  1  2  6  6  6  4  4  5  2  3  6  1  6  4  3      2        NA  14
## 61672  2  5  1  3  5  5  4  5  2  5  1  2  6  5  4  1  4  2  2  5  2  4  5  4  1      2         3  33
## 61673  4  5  6  5  5  5  5  3  5  4  1  2  6  5  5  5  4  4  3  1  4  4  6  5  1      2         3  18
## 61678  1  6  5  6  3  5  5  5  4  3  2  5  1  5  3  5  5  5  6  6  4  3  3  6  5      2        NA  17
## 61679  2  5  6  6  6  5  5  5  2  4  1  2  4  5  5  3  2  4  1  2  5  2  5  5  2      2         3  41
## 61682  1  5  6  5  4  1  5  6  4  6  6  6  2  1  1  1  2  1  3  6  6  6  5  6  1      1         5  23
## 61683  2  4  5  6  5  4  6  4  2  4  2  2  3  5  3  2  2  4  1  3  5  5  5  4  2      2        NA  17
## 61684  4  4  4  4  4  4  3  3  3  4  2  3  4  2  3 NA  2  1  2  2  4  3  5  5  3      1         3  20
##  [ reached 'max' / getOption("max.print") -- omitted 2765 rows ]
sum(bfi$gender == 2)
## [1] 1881
bfi %>%
  filter(gender == 1) %>%
  count(gender)
##   gender   n
## 1      1 919
bfi$gender[gender==1]
##   [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
##  [63] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [125] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [187] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1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