R 언어 기본 공부 1
v.1 <- c("PI", 3.14, TRUE)
v.1
'PI'
'3.14'
'TRUE'
d <- c(1,3,2,4,5)
d
1
3
2
4
5
sort(d)
1
2
3
4
5
sort(d, decreasing = T) #내림차순
5
4
3
2
1
V.1 <- c()
V.1
NULL
V.1[1] <- 10
V.1
10
V.1[5] <- 50
V.1
10
<NA>
<NA>
<NA>
50
V.2 <- c(v.1, seq(60,90,10))
V.2
'PI'
'3.14'
'TRUE'
'60'
'70'
'80'
'90'
v.1 <- 1:9
v.1
1
2
3
4
5
6
7
8
9
v.2 <- v.1 >5
v.2
FALSE
FALSE
FALSE
FALSE
FALSE
TRUE
TRUE
TRUE
TRUE
v.3 <- (v.1 %% 2 == 0)
v.3
FALSE
TRUE
FALSE
TRUE
FALSE
TRUE
FALSE
TRUE
FALSE
v.1[v.3]
2
4
6
8
#요인객체 생성
f <- factor(c("low","high"))
f
<ol class=list-inline> <li>low</li> <li>high</li> </ol>
## gl 함수 : 요인 수열의 생성
f.1 <- gl(2,6,labels=c("male","female"))
f.1
<ol class=list-inline> <li>male</li> <li>male</li> <li>male</li> <li>male</li> <li>male</li> <li>male</li> <li>female</li> <li>female</li> <li>female</li> <li>female</li> <li>female</li> <li>female</li> </ol>
#### gl 함수 : 요인 수열의 생성
f.1 <- gl(1,6,labels=c("male","female"))
f.1
<ol class=list-inline> <li>male</li> <li>male</li> <li>male</li> <li>male</li> <li>male</li> <li>male</li> </ol>
f.1 <- gl(0,7,labels=c("male","female"))
f.1
##table() 함수 : 요인객체에 빈도표 작성
table(f.1)
f.1
male female
6 0
f.2 <- factor(c("white","black","white","white","white","white"))
(t <- table(f.1,f.2)) #table = t
f.2
f.1 black white
male 1 5
female 0 0
##margin.table() :주변표 작성
margin.table(t, 1) #1은 행
f.1
male female
6 0
margin.table(t,2) #2는 열
f.2
black white
1 5
##prop .table(): 비율표 작성
prop.table(t, 1)
f.2
f.1 black white
male 0.1666667 0.8333333
female
prop.table(t, 2)
f.2
f.1 black white
male 1 1
female 0 0
# margin.table은 합, prop.table은 백분율
prop.table(t)
f.2
f.1 black white
male 0.1666667 0.8333333
female 0.0000000 0.0000000
##matrix() 함수: 행렬 겍체 생성
m <- matrix(c(1,2,3,4,5,6,7,8),2,4)
m
1 | 3 | 5 | 7 |
2 | 4 | 6 | 8 |
m <- matrix(c(22,33,44),1,3)
m
22 | 33 | 44 |
m[1,3]
44
m[1,]
<ol class=list-inline> <li>22</li> <li>33</li> <li>44</li> </ol>
m[1,-3]
<ol class=list-inline> <li>22</li> <li>33</li> </ol>
m[1,-2]
<ol class=list-inline> <li>22</li> <li>44</li> </ol>
##array 함수 :배열 객체 생성 ,3차원 배열
a <- array(1:30, c(2,5,3))
a
<ol class=list-inline> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> <li>8</li> <li>9</li> <li>10</li> <li>11</li> <li>12</li> <li>13</li> <li>14</li> <li>15</li> <li>16</li> <li>17</li> <li>18</li> <li>19</li> <li>20</li> <li>21</li> <li>22</li> <li>23</li> <li>24</li> <li>25</li> <li>26</li> <li>27</li> <li>28</li> <li>29</li> <li>30</li> </ol>
#list는 서로 다른 유형의 객체들의 순서화된 모임
l <- list(id=c(1,2,3), name=c("nam","dk","wd"),score=c(70,80,90))
l
- $id
- <ol class=list-inline>
- 1
- 2
- 3
</ol> - $name
- <ol class=list-inline>
- 'nam'
- 'dk'
- 'wd'
</ol> - $score
- <ol class=list-inline>
- 70
- 80
- 90
</ol>
l$name
<ol class=list-inline> <li>‘nam’</li> <li>‘dk’</li> <li>‘wd’</li> </ol>
l$score
<ol class=list-inline> <li>70</li> <li>80</li> <li>90</li> </ol>
l[3]
$score
70
80
90
l[[3]]
<ol class=list-inline> <li>70</li> <li>80</li> <li>90</li> </ol>
l[[2]]
<ol class=list-inline> <li>‘nam’</li> <li>‘dk’</li> <li>‘wd’</li> </ol>
l$name
<ol class=list-inline> <li>‘nam’</li> <li>‘dk’</li> <li>‘wd’</li> </ol>
unlist(l)
<dl class=dl-horizontal> <dt>id1</dt> <dd>‘1’</dd> <dt>id2</dt> <dd>‘2’</dd> <dt>id3</dt> <dd>‘3’</dd> <dt>name1</dt> <dd>‘nam’</dd> <dt>name2</dt> <dd>‘dk’</dd> <dt>name3</dt> <dd>‘wd’</dd> <dt>score1</dt> <dd>‘70’</dd> <dt>score2</dt> <dd>‘80’</dd> <dt>score3</dt> <dd>‘90’</dd> </dl>
#data.frame() 함수: 데이터프레임 객체 생성
d <- data.frame(id=c(1,2,3),name=c("kim","dd","qq"),score=c(11,22,33))
d
id | name | score |
---|---|---|
1 | kim | 11 |
2 | dd | 22 |
3 | 33 |
d$id
<ol class=list-inline> <li>1</li> <li>2</li> <li>3</li> </ol>
d$name
<ol class=list-inline> <li>kim</li> <li>dd</li> <li>qq</li> </ol>
d$score
<ol class=list-inline> <li>11</li> <li>22</li> <li>33</li> </ol>
d[2,3]
22
d[1,]
id | name | score |
---|---|---|
1 | kim | 11 |
d[,1]
<ol class=list-inline> <li>1</li> <li>2</li> <li>3</li> </ol>
d[d$score >= 20,] # ,을 꼭 필요로 한다
id | name | score | |
---|---|---|---|
2 | 2 | dd | 22 |
3 | 3 | 33 |
d$name=="kim"
<ol class=list-inline> <li>TRUE</li> <li>FALSE</li> <li>FALSE</li> </ol>
d[d$name=="kim",]
id | name | score |
---|---|---|
1 | kim | 11 |
d$name=="dd"
<ol class=list-inline> <li>FALSE</li> <li>TRUE</li> <li>FALSE</li> </ol>
d[d$name=="dd",]
id | name | score | |
---|---|---|---|
2 | 2 | dd | 22 |
d[d$name=="dd","score"]
22
d[d$name=="dd","id"]
2
d[d$name=="dd",c("id","score")]
id | score | |
---|---|---|
2 | 2 | 22 |
names(d)
<ol class=list-inline> <li>‘id’</li> <li>‘name’</li> <li>‘score’</li> </ol>
names(d)[3]<- "total"
d
id | name | total |
---|---|---|
1 | kim | 11 |
2 | dd | 22 |
3 | 33 |
names(d)
'id'
'name'
'total'
names(d)[1] <- "num"
d
num | name | total |
---|---|---|
1 | kim | 11 |
2 | dd | 22 |
3 | 33 |