data(iris)
head(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
head(iris,3)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
tail(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 145 6.7 3.3 5.7 2.5 virginica
## 146 6.7 3.0 5.2 2.3 virginica
## 147 6.3 2.5 5.0 1.9 virginica
## 148 6.5 3.0 5.2 2.0 virginica
## 149 6.2 3.4 5.4 2.3 virginica
## 150 5.9 3.0 5.1 1.8 virginica
tail(iris,3)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 148 6.5 3.0 5.2 2.0 virginica
## 149 6.2 3.4 5.4 2.3 virginica
## 150 5.9 3.0 5.1 1.8 virginica
str(iris)
## 'data.frame': 150 obs. of 5 variables:
## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
dim(iris)
## [1] 150 5
length(iris)
## [1] 5
class(iris)
## [1] "data.frame"
is(iris)
## [1] "data.frame" "list" "oldClass" "vector"
names(iris)
## [1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species"
is(iris)
## [1] "data.frame" "list" "oldClass" "vector"
names(iris)
## [1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species"
colnames(iris)
## [1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species"
str(mtcars)
## '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 ...
dim(mtcars)
## [1] 32 11
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
tail(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.7 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.9 1 1 5 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.5 0 1 5 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.5 0 1 5 6
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.6 0 1 5 8
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.6 1 1 4 2
mean(mtcars$mpg)
## [1] 20.09062
var(mtcars$mpg)
## [1] 36.3241
sd(mtcars$mpg)
## [1] 6.026948
sum(mtcars$mpg)
## [1] 642.9
sqrt(36.3241) # 제곱근 마킹킹
## [1] 6.026948
max(mtcars$mpg)
## [1] 33.9
# 사분위수 범위 상위25% 와 75% 의 평균값 반드시 수치형일때 씀씀
library(gapminder)
## Warning: 패키지 'gapminder'는 R 버전 4.3.2에서 작성되었습니다
str(gapminder)
## tibble [1,704 × 6] (S3: tbl_df/tbl/data.frame)
## $ country : Factor w/ 142 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ continent: Factor w/ 5 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
## $ year : int [1:1704] 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
## $ lifeExp : num [1:1704] 28.8 30.3 32 34 36.1 ...
## $ pop : int [1:1704] 8425333 9240934 10267083 11537966 13079460 14880372 12881816 13867957 16317921 22227415 ...
## $ gdpPercap: num [1:1704] 779 821 853 836 740 ...
df<-read.csv("Data1.csv")
str(df)
## 'data.frame': 1925 obs. of 26 variables:
## $ Q1 : int 4 4 4 5 4 4 4 4 4 4 ...
## $ Q2 : int 4 4 4 4 4 4 2 2 4 4 ...
## $ Q3 : int 2 4 4 4 4 4 4 4 4 2 ...
## $ Q4 : int 3 4 4 4 4 4 4 4 4 2 ...
## $ Q5 : int 4 4 2 4 4 4 4 4 2 4 ...
## $ Q6 : int 2 3 4 4 4 4 4 4 1 2 ...
## $ Q7 : int 2 2 4 4 4 4 4 4 3 4 ...
## $ Q8 : int 4 4 4 4 4 4 5 5 2 2 ...
## $ Q9 : int 4 4 4 4 2 4 5 5 3 4 ...
## $ Q10 : int 4 4 2 4 4 4 5 5 2 4 ...
## $ Q11 : int 4 4 4 4 4 4 5 5 4 4 ...
## $ Q12 : int 4 4 4 4 4 4 5 5 3 4 ...
## $ Q13 : int 4 4 4 4 4 4 5 5 4 4 ...
## $ Q14 : int 4 4 4 4 4 4 5 5 5 4 ...
## $ Q15 : int 4 4 3 4 4 4 4 2 3 4 ...
## $ Q16 : int 4 4 4 4 4 4 5 2 4 4 ...
## $ Q17 : int 4 3 4 4 4 4 2 2 4 4 ...
## $ Q18 : int 4 4 4 4 4 4 4 4 4 4 ...
## $ Q19 : int 4 2 4 4 4 4 4 2 4 2 ...
## $ Q20 : int 4 1 3 4 4 4 4 2 4 2 ...
## $ Gender1 : int 0 0 0 0 0 0 0 0 1 0 ...
## $ EDU1 : int 1 1 2 1 2 1 1 1 4 3 ...
## $ BF : num 3.4 4 3.6 4.2 4 4 3.6 3.6 3.6 3.2 ...
## $ BM : num 3.2 3.4 3.6 4 3.6 4 4.6 4.6 2.2 3.2 ...
## $ Happiness: num 4 4 3.8 4 4 4 4.8 4.4 3.8 4 ...
## $ Peace : num 4 2.8 3.8 4 4 4 3.8 2.4 4 3.2 ...
df$Gender1<-factor(df$Gender1)
df$EDU1<-factor(df$EDU1)
levels(df$Gender1)
## [1] "0" "1"
levels(df$EDU1)
## [1] "1" "2" "3" "4"
library(plyr)
## Warning: 패키지 'plyr'는 R 버전 4.3.2에서 작성되었습니다
df$Gender1<-revalue(df$Gender1,replace=c("0"="여성","1"="남성"))
table(df$Gender1)
##
## 여성 남성
## 1136 789
df$EDU1<-revalue(df$EDU1,replace = c("1"="고졸", "2"="재학중","3"="대졸","4"="대학원졸"))
table(df$EDU1)
##
## 고졸 재학중 대졸 대학원졸
## 233 472 1022 198
table(df$Gender1,df$EDU1)
##
## 고졸 재학중 대졸 대학원졸
## 여성 182 277 582 95
## 남성 51 195 440 103
a<-table(df$Gender1)
b<-table(df$EDU1)
prop.table(a)
##
## 여성 남성
## 0.5901299 0.4098701
prop.table(b)
##
## 고졸 재학중 대졸 대학원졸
## 0.1210390 0.2451948 0.5309091 0.1028571
e<-table(df$Gender1,df$EDU1)
prop.table(e)
##
## 고졸 재학중 대졸 대학원졸
## 여성 0.09454545 0.14389610 0.30233766 0.04935065
## 남성 0.02649351 0.10129870 0.22857143 0.05350649
prop.table(e,1) #행의 합의 1일되도록 비율표시 ,2는 행의 열이 1이되도록록
##
## 고졸 재학중 대졸 대학원졸
## 여성 0.16021127 0.24383803 0.51232394 0.08362676
## 남성 0.06463878 0.24714829 0.55766793 0.13054499
prop.table(e,2)
##
## 고졸 재학중 대졸 대학원졸
## 여성 0.7811159 0.5868644 0.5694716 0.4797980
## 남성 0.2188841 0.4131356 0.4305284 0.5202020
round(0.7851159,2) # 소수2자리까지 표시시
## [1] 0.79
round(0.7851159,2)
## [1] 0.79
round(prop.table(e),2) # 소수2자리까지 표시시행행
##
## 고졸 재학중 대졸 대학원졸
## 여성 0.09 0.14 0.30 0.05
## 남성 0.03 0.10 0.23 0.05
round(prop.table(e),2)
##
## 고졸 재학중 대졸 대학원졸
## 여성 0.09 0.14 0.30 0.05
## 남성 0.03 0.10 0.23 0.05