30. R ์ฃผ์ฑ๋ถ๋ถ์ ์ฐ์ต๋ฌธ์
01. mtcars ๋ฐ์ดํฐ์ ์ ๋์์ผ๋ก ์ฐ๋นํจ์จ(mpg), ์ค๋ฆฐ๋์(cyl), ์์งํฌ๊ธฐ(disp), ๋ง๋ ฅ(hp), ๋ฌด๊ฒ(wt) ๋ณ์๋ฅผ ๋์์ผ๋ก ์๋ธ์ ์ ์์ฑํ์์ค.
library(datasets)
data(mtcars)
str(mtcars)
์นผ๋ผ๋ช
์ด์ฉ
mtcars_df <- mtcars[c('mpg','cyl','disp','hp','wt')]
str(mtcars_df)
์์ธ(index) ์ด์ฉ
mtcars_df2 <- mtcars[c(1:4,6)]
str(mtcars_df2)
02. ์์ฑ๋ ์๋ธ์
์ ๋์์ผ๋ก ์๊ด๋ถ์์ ์ํํ์ฌ ์ฐ๋นํจ์จ(mpg)๊ณผ ๊ฐ์ฅ ์๊ด๊ณ์๊ฐ ๋์ ๋ณ์๋ฅผ ํ์ธํ์์ค.
COR <- cor(mtcars_df)
COR['mpg',]
mpg cyl disp hp wt
1.0000000 -0.8521620 -0.8475514 -0.7761684 -0.8676594
03. ์ฐ๋นํจ์จ๊ณผ ๊ฐ์ฅ ์๊ด๊ณ์๊ฐ ๋์ ๋ณ์์ ์ฐ์ ๋๋ก ์๊ฐํํ์์ค. ํํธ) plot()ํจ์ ์ด์ฉ
plot(mtcars_df$mpg, mtcars_df$wt)
[ํด์ค] ๋ ๋ณ์๋ ์์ ์๊ด๊ณ์๋ฅผ ๋ณด์ธ๋ค.
04. iris ๋ฐ์ดํฐ์
์์ 5๋ฒ์งธ ์นผ๋ผ์ ์ ์ธํ 4๊ฐ์ ์นผ๋ผ์ผ๋ก ์๊ด๊ณ์๋ฅผ ํ์ธํ์์ค.
<๋จ๊ณ1> 4๊ฐ ์นผ๋ผ ๊ฐ์ ์๊ด๊ณ์ ํ๋ ฌ ํ์ธ
data("iris")
cor(iris[-5])
Sepal.Length Sepal.Width Petal.Length Petal.Width
Sepal.Length 1.0000000 -0.1175698 0.8717538 0.8179411
Sepal.Width -0.1175698 1.0000000 -0.4284401 -0.3661259
Petal.Length 0.8717538 -0.4284401 1.0000000 0.9628654
Petal.Width 0.8179411 -0.3661259 0.9628654 1.0000000
<๋จ๊ณ2> ์ฒซ๋ฒ์งธ ์นผ๋ผ(Sepal.Length) ๊ธฐ์ค์ผ๋ก ๋๋จธ์ง ๋ณ์์ ์๊ด๊ณ์ ์ถ๋ ฅ
COR <- cor(iris[-5])
COR['Sepal.Length', ]
Sepal.Length Sepal.Width Petal.Length Petal.Width
1.0000000 -0.1175698 0.8717538 0.8179411
<๋จ๊ณ3> ์์ ์๊ด๊ณ์๊ฐ ๊ฐ์ฅ ํฐ ๋ ๋ณ์๋ฅผ ๋์์ผ๋ก ์ฐ์ ๋ ์๊ฐํ
<์กฐ๊ฑด1> qplot()ํจ์ ์ด์ฉ
<์กฐ๊ฑด2> Species ๋ณ์๋ก ์์ ์ ์ฉ
library(ggplot2)
3๋ฒ์งธ์ 4๋ฒ์งธ ๋ณ์ ๋์ ์ฐ์ ๋
qplot(Petal.Length, Petal.Width, data = iris, color = Species)