빅데이터 시각화 4주차 강의
x <-1
x
1
y <-2
y
2
# 변경됨
temp =x
x = y
y =temp
y
1
xi = 1+2i
yi =1-2i
xi+yi
2+0i
str = "heel"
str
‘heel’
blood.type=factor(c('a','b','o','ab'))
blood.type
<ol class=list-inline> <li>a</li> <li>b</li> <li>o</li> <li>ab</li> </ol>
#NaN
xinf=Inf
yinf=-Inf
xinf/yinf
NaN
is.integer(1)
FALSE
is.numeric(1)
TRUE
is.integer(1L) #정수 형 L붙이기
TRUE
x=1
is.integer(x)
FALSE
x=1L
is.integer(x) #true
TRUE
1:7
<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> </ol>
7:1
<ol class=list-inline> <li>7</li> <li>6</li> <li>5</li> <li>4</li> <li>3</li> <li>2</li> <li>1</li> </ol>
c(1:5)
<ol class=list-inline> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ol>
c(1,2,3,c(4:6))
<ol class=list-inline> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> </ol>
x=c(1,2,3)
x
<ol class=list-inline> <li>1</li> <li>2</li> <li>3</li> </ol>
y=c()
y=c(y,c(1:3))
y
<ol class=list-inline> <li>1</li> <li>2</li> <li>3</li> </ol>
seq(1,10,2)
<ol class=list-inline> <li>1</li> <li>3</li> <li>5</li> <li>7</li> <li>9</li> </ol>
seq(1,10,by=2)
<ol class=list-inline> <li>1</li> <li>3</li> <li>5</li> <li>7</li> <li>9</li> </ol>
seq(0,1,by=0.1)
<ol class=list-inline> <li>0</li> <li>0.1</li> <li>0.2</li> <li>0.3</li> <li>0.4</li> <li>0.5</li> <li>0.6</li> <li>0.7</li> <li>0.8</li> <li>0.9</li> <li>1</li> </ol>
seq(0,1,length.out=11)##11개 값생성
<ol class=list-inline> <li>0</li> <li>0.1</li> <li>0.2</li> <li>0.3</li> <li>0.4</li> <li>0.5</li> <li>0.6</li> <li>0.7</li> <li>0.8</li> <li>0.9</li> <li>1</li> </ol>
rep(c(1:3),times=2)
<ol class=list-inline> <li>1</li> <li>2</li> <li>3</li> <li>1</li> <li>2</li> <li>3</li> </ol>
rep(c(1:3),times=3)
<ol class=list-inline> <li>1</li> <li>2</li> <li>3</li> <li>1</li> <li>2</li> <li>3</li> <li>1</li> <li>2</li> <li>3</li> </ol>
rep(c(1:3),each=2)
<ol class=list-inline> <li>1</li> <li>1</li> <li>2</li> <li>2</li> <li>3</li> <li>3</li> </ol>
x=c(2,4,6,8,10)
length(x)
5
x[1]
2
x[c(1,2,3)]
<ol class=list-inline> <li>2</li> <li>4</li> <li>6</li> </ol>
x[-c(1,2,3)] #1,2,3 주소값 제거 후 결과 출력력
<ol class=list-inline> <li>8</li> <li>10</li> </ol>
x[c(1:3)]
<ol class=list-inline> <li>2</li> <li>4</li> <li>6</li> </ol>
x= c(1,2,3,4)
y= c(5,6,7,8)
z=c(3,4)
w=c(5,6,7)
x+2
<ol class=list-inline> <li>3</li> <li>4</li> <li>5</li> <li>6</li> </ol>
x+y
<ol class=list-inline> <li>6</li> <li>8</li> <li>10</li> <li>12</li> </ol>
x+z
<ol class=list-inline> <li>4</li> <li>6</li> <li>6</li> <li>8</li> </ol>
x+w
Warning message in x + w:
"longer object length is not a multiple of shorter object length"
<ol class=list-inline> <li>6</li> <li>8</li> <li>10</li> <li>9</li> </ol>
x=c(1,2,3,4)
y=c(11,22,33)
z=c(3,44)
w=c(44,11,22)
x+2
x+5
x+z
<ol class=list-inline> <li>3</li> <li>4</li> <li>5</li> <li>6</li> </ol>
<ol class=list-inline> <li>6</li> <li>7</li> <li>8</li> <li>9</li> </ol>
<ol class=list-inline> <li>4</li> <li>46</li> <li>6</li> <li>48</li> </ol>
x=1:20
x
<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> </ol>
x>4
<ol class=list-inline> <li>FALSE</li> <li>FALSE</li> <li>FALSE</li> <li>FALSE</li> <li>TRUE</li> <li>TRUE</li> <li>TRUE</li> <li>TRUE</li> <li>TRUE</li> <li>TRUE</li> <li>TRUE</li> <li>TRUE</li> <li>TRUE</li> <li>TRUE</li> <li>TRUE</li> <li>TRUE</li> <li>TRUE</li> <li>TRUE</li> <li>TRUE</li> <li>TRUE</li> </ol>
all(x>4) #모두 4보다 크나
FALSE
any(x>4)
TRUE
a=1:10
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> </ol>
head(a) #데이터 앞 6개 출력
<ol class=list-inline> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> </ol>
tail(a) #데이터를 뒤에서 6개 출력
<ol class=list-inline> <li>5</li> <li>6</li> <li>7</li> <li>8</li> <li>9</li> <li>10</li> </ol>
head(a,3)
<ol class=list-inline> <li>1</li> <li>2</li> <li>3</li> </ol>
tail(a,3)
<ol class=list-inline> <li>8</li> <li>9</li> <li>10</li> </ol>
x = c(1,2,3)
y = c(3,4,5)
z = c(3,1,2)
union(x,y) #합집합
<ol class=list-inline> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ol>
intersect(x,y) #교집합
3
setdiff(x,y) # x에서 y와 동일한 값 제거
<ol class=list-inline> <li>1</li> <li>2</li> </ol>
setdiff(y,x)
<ol class=list-inline> <li>4</li> <li>5</li> </ol>
setequal(x,y)
FALSE
setequal(x,z)
TRUE
x = array(1:5,c(2,4))
x
1 | 3 | 5 | 2 |
2 | 4 | 1 | 3 |
x[1,]
<ol class=list-inline> <li>1</li> <li>3</li> <li>5</li> <li>2</li> </ol>
x[,2]
<ol class=list-inline> <li>3</li> <li>4</li> </ol>
x[,3]
x[,4]
<ol class=list-inline> <li>5</li> <li>1</li> </ol>
<ol class=list-inline> <li>2</li> <li>3</li> </ol>
x=1:12
x
<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> </ol>
matrix(x,nrow = 3)
1 | 4 | 7 | 10 |
2 | 5 | 8 | 11 |
3 | 6 | 9 | 12 |
matrix(x,nrow = 5)
Warning message in matrix(x, nrow = 5):
"data length [12] is not a sub-multiple or multiple of the number of rows [5]"
1 | 6 | 11 |
2 | 7 | 12 |
3 | 8 | 1 |
4 | 9 | 2 |
5 | 10 | 3 |
matrix(x,nrow = 3,byrow = T)
1 | 2 | 3 | 4 |
5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 |
V1=c(1,2,3,4)
v2=c(5,6,7,8)
V3=c(9,10,11,12)
cbind(V1,v2,V3)
rbind(V1,v2,V3)
V1 | v2 | V3 |
---|---|---|
1 | 5 | 9 |
2 | 6 | 10 |
3 | 7 | 11 |
4 | 8 | 12 |
V1 | 1 | 2 | 3 | 4 |
---|---|---|---|---|
v2 | 5 | 6 | 7 | 8 |
V3 | 9 | 10 | 11 | 12 |
a=array(1:4,c(2,2))
a
y=array(5:8,c(2,2))
y
1 | 3 |
2 | 4 |
5 | 7 |
6 | 8 |
a+y
a-y
6 | 10 |
8 | 12 |
-4 | -4 |
-4 | -4 |
a%*%y #행렬 곱셈
23 | 31 |
34 | 46 |
t(a) #전치행렬
1 | 2 |
3 | 4 |
x=array(1:12,c(3,4))
x
1 | 4 | 7 | 10 |
2 | 5 | 8 | 11 |
3 | 6 | 9 | 12 |
apply(x,1,mean) #행별 평균
<ol class=list-inline> <li>5.5</li> <li>6.5</li> <li>7.5</li> </ol>
apply(x,2,mean) #열 별 평균
<ol class=list-inline> <li>2</li> <li>5</li> <li>8</li> <li>11</li> </ol>
x=array(1:12,c(3,4))
x
1 | 4 | 7 | 10 |
2 | 5 | 8 | 11 |
3 | 6 | 9 | 12 |
sample(x)
<ol class=list-inline> <li>7</li> <li>10</li> <li>11</li> <li>9</li> <li>4</li> <li>2</li> <li>8</li> <li>3</li> <li>6</li> <li>1</li> <li>5</li> <li>12</li> </ol>
sample(x,10,prob = c(1:12)/24) #확률 달리 해서 추출하기/#가중치를 고려한 표본추출
<ol class=list-inline> <li>11</li> <li>2</li> <li>9</li> <li>4</li> <li>6</li> <li>10</li> <li>8</li> <li>5</li> <li>12</li> <li>7</li> </ol>
sample(42)
<ol class=list-inline> <li>16</li> <li>2</li> <li>32</li> <li>5</li> <li>30</li> <li>15</li> <li>18</li> <li>13</li> <li>23</li> <li>17</li> <li>10</li> <li>3</li> <li>28</li> <li>26</li> <li>42</li> <li>39</li> <li>31</li> <li>35</li> <li>12</li> <li>14</li> <li>19</li> <li>22</li> <li>1</li> <li>7</li> <li>33</li> <li>41</li> <li>8</li> <li>11</li> <li>21</li> <li>25</li> <li>27</li> <li>24</li> <li>6</li> <li>40</li> <li>36</li> <li>9</li> <li>37</li> <li>38</li> <li>29</li> <li>20</li> <li>4</li> <li>34</li> </ol>
name=c('철수','춘향','길동')
age=c(22,20,25)
gender=factor(c('m','f','m'))
blood.type=factor(c('A','B','O'))
patients=data.frame(name,age,gender,blood.type)
patients
name | age | gender | blood.type |
---|---|---|---|
철수 | 22 | m | A |
춘향 | 20 | f | B |
길동 | 25 | m | O |
patients$name
<ol class=list-inline> <li>철수</li> <li>춘향</li> <li>길동</li> </ol>
patients[1,]
name | age | gender | blood.type |
---|---|---|---|
철수 | 22 | m | A |
patients[,2]
<ol class=list-inline> <li>22</li> <li>20</li> <li>25</li> </ol>
patients[3,1]
길동
patients[patients$name=="철수",]
name | age | gender | blood.type |
---|---|---|---|
철수 | 22 | m | A |
patients[patients$name=="철수",c("name","age")]
name | age |
---|---|
철수 | 22 |
head(cars)
speed | dist |
---|---|
4 | 2 |
4 | 10 |
7 | 4 |
7 | 22 |
8 | 16 |
9 | 10 |
attach(cars)
speed
<ol class=list-inline> <li>4</li> <li>4</li> <li>7</li> <li>7</li> <li>8</li> <li>9</li> <li>10</li> <li>10</li> <li>10</li> <li>11</li> <li>11</li> <li>12</li> <li>12</li> <li>12</li> <li>12</li> <li>13</li> <li>13</li> <li>13</li> <li>13</li> <li>14</li> <li>14</li> <li>14</li> <li>14</li> <li>15</li> <li>15</li> <li>15</li> <li>16</li> <li>16</li> <li>17</li> <li>17</li> <li>17</li> <li>18</li> <li>18</li> <li>18</li> <li>18</li> <li>19</li> <li>19</li> <li>19</li> <li>20</li> <li>20</li> <li>20</li> <li>20</li> <li>20</li> <li>22</li> <li>23</li> <li>24</li> <li>24</li> <li>24</li> <li>24</li> <li>25</li> </ol>
dist
<ol class=list-inline> <li>2</li> <li>10</li> <li>4</li> <li>22</li> <li>16</li> <li>10</li> <li>18</li> <li>26</li> <li>34</li> <li>17</li> <li>28</li> <li>14</li> <li>20</li> <li>24</li> <li>28</li> <li>26</li> <li>34</li> <li>34</li> <li>46</li> <li>26</li> <li>36</li> <li>60</li> <li>80</li> <li>20</li> <li>26</li> <li>54</li> <li>32</li> <li>40</li> <li>32</li> <li>40</li> <li>50</li> <li>42</li> <li>56</li> <li>76</li> <li>84</li> <li>36</li> <li>46</li> <li>68</li> <li>32</li> <li>48</li> <li>52</li> <li>56</li> <li>64</li> <li>66</li> <li>54</li> <li>70</li> <li>92</li> <li>93</li> <li>120</li> <li>85</li> </ol>
str(cars)
'data.frame': 50 obs. of 2 variables:
$ speed: num 4 4 7 7 8 9 10 10 10 11 ...
$ dist : num 2 10 4 22 16 10 18 26 34 17 ...
detach(cars)
mean(cars$speed)
15.4
max(cars$speed)
25
with(cars,max(speed))
25
#subset 일부만 추출한다
subset(cars,speed>20)
speed | dist | |
---|---|---|
44 | 22 | 66 |
45 | 23 | 54 |
46 | 24 | 70 |
47 | 24 | 92 |
48 | 24 | 93 |
49 | 24 | 120 |
50 | 25 | 85 |
subset(cars,speed>20,select=c(dist))
dist | |
---|---|
44 | 66 |
45 | 54 |
46 | 70 |
47 | 92 |
48 | 93 |
49 | 120 |
50 | 85 |
subset(cars,speed>10,select=-c(speed))#speed 제외
dist | |
---|---|
10 | 17 |
11 | 28 |
12 | 14 |
13 | 20 |
14 | 24 |
15 | 28 |
16 | 26 |
17 | 34 |
18 | 34 |
19 | 46 |
20 | 26 |
21 | 36 |
22 | 60 |
23 | 80 |
24 | 20 |
25 | 26 |
26 | 54 |
27 | 32 |
28 | 40 |
29 | 32 |
30 | 40 |
31 | 50 |
32 | 42 |
33 | 56 |
34 | 76 |
35 | 84 |
36 | 36 |
37 | 46 |
38 | 68 |
39 | 32 |
40 | 48 |
41 | 52 |
42 | 56 |
43 | 64 |
44 | 66 |
45 | 54 |
46 | 70 |
47 | 92 |
48 | 93 |
49 | 120 |
50 | 85 |
airquality
Ozone | Solar.R | Wind | Temp | Month | Day |
---|---|---|---|---|---|
41 | 190 | 7.4 | 67 | 5 | 1 |
36 | 118 | 8.0 | 72 | 5 | 2 |
12 | 149 | 12.6 | 74 | 5 | 3 |
18 | 313 | 11.5 | 62 | 5 | 4 |
NA | NA | 14.3 | 56 | 5 | 5 |
28 | NA | 14.9 | 66 | 5 | 6 |
23 | 299 | 8.6 | 65 | 5 | 7 |
19 | 99 | 13.8 | 59 | 5 | 8 |
8 | 19 | 20.1 | 61 | 5 | 9 |
NA | 194 | 8.6 | 69 | 5 | 10 |
7 | NA | 6.9 | 74 | 5 | 11 |
16 | 256 | 9.7 | 69 | 5 | 12 |
11 | 290 | 9.2 | 66 | 5 | 13 |
14 | 274 | 10.9 | 68 | 5 | 14 |
18 | 65 | 13.2 | 58 | 5 | 15 |
14 | 334 | 11.5 | 64 | 5 | 16 |
34 | 307 | 12.0 | 66 | 5 | 17 |
6 | 78 | 18.4 | 57 | 5 | 18 |
30 | 322 | 11.5 | 68 | 5 | 19 |
11 | 44 | 9.7 | 62 | 5 | 20 |
1 | 8 | 9.7 | 59 | 5 | 21 |
11 | 320 | 16.6 | 73 | 5 | 22 |
4 | 25 | 9.7 | 61 | 5 | 23 |
32 | 92 | 12.0 | 61 | 5 | 24 |
NA | 66 | 16.6 | 57 | 5 | 25 |
NA | 266 | 14.9 | 58 | 5 | 26 |
NA | NA | 8.0 | 57 | 5 | 27 |
23 | 13 | 12.0 | 67 | 5 | 28 |
45 | 252 | 14.9 | 81 | 5 | 29 |
115 | 223 | 5.7 | 79 | 5 | 30 |
... | ... | ... | ... | ... | ... |
96 | 167 | 6.9 | 91 | 9 | 1 |
78 | 197 | 5.1 | 92 | 9 | 2 |
73 | 183 | 2.8 | 93 | 9 | 3 |
91 | 189 | 4.6 | 93 | 9 | 4 |
47 | 95 | 7.4 | 87 | 9 | 5 |
32 | 92 | 15.5 | 84 | 9 | 6 |
20 | 252 | 10.9 | 80 | 9 | 7 |
23 | 220 | 10.3 | 78 | 9 | 8 |
21 | 230 | 10.9 | 75 | 9 | 9 |
24 | 259 | 9.7 | 73 | 9 | 10 |
44 | 236 | 14.9 | 81 | 9 | 11 |
21 | 259 | 15.5 | 76 | 9 | 12 |
28 | 238 | 6.3 | 77 | 9 | 13 |
9 | 24 | 10.9 | 71 | 9 | 14 |
13 | 112 | 11.5 | 71 | 9 | 15 |
46 | 237 | 6.9 | 78 | 9 | 16 |
18 | 224 | 13.8 | 67 | 9 | 17 |
13 | 27 | 10.3 | 76 | 9 | 18 |
24 | 238 | 10.3 | 68 | 9 | 19 |
16 | 201 | 8.0 | 82 | 9 | 20 |
13 | 238 | 12.6 | 64 | 9 | 21 |
23 | 14 | 9.2 | 71 | 9 | 22 |
36 | 139 | 10.3 | 81 | 9 | 23 |
7 | 49 | 10.3 | 69 | 9 | 24 |
14 | 20 | 16.6 | 63 | 9 | 25 |
30 | 193 | 6.9 | 70 | 9 | 26 |
NA | 145 | 13.2 | 77 | 9 | 27 |
14 | 191 | 14.3 | 75 | 9 | 28 |
18 | 131 | 8.0 | 76 | 9 | 29 |
20 | 223 | 11.5 | 68 | 9 | 30 |
head(airquality)
Ozone | Solar.R | Wind | Temp | Month | Day |
---|---|---|---|---|---|
41 | 190 | 7.4 | 67 | 5 | 1 |
36 | 118 | 8.0 | 72 | 5 | 2 |
12 | 149 | 12.6 | 74 | 5 | 3 |
18 | 313 | 11.5 | 62 | 5 | 4 |
NA | NA | 14.3 | 56 | 5 | 5 |
28 | NA | 14.9 | 66 | 5 | 6 |
head(na.omit(airquality)) #na값 제거
Ozone | Solar.R | Wind | Temp | Month | Day | |
---|---|---|---|---|---|---|
1 | 41 | 190 | 7.4 | 67 | 5 | 1 |
2 | 36 | 118 | 8.0 | 72 | 5 | 2 |
3 | 12 | 149 | 12.6 | 74 | 5 | 3 |
4 | 18 | 313 | 11.5 | 62 | 5 | 4 |
7 | 23 | 299 | 8.6 | 65 | 5 | 7 |
8 | 19 | 99 | 13.8 | 59 | 5 | 8 |
na.omit(airquality)
Ozone | Solar.R | Wind | Temp | Month | Day | |
---|---|---|---|---|---|---|
1 | 41 | 190 | 7.4 | 67 | 5 | 1 |
2 | 36 | 118 | 8.0 | 72 | 5 | 2 |
3 | 12 | 149 | 12.6 | 74 | 5 | 3 |
4 | 18 | 313 | 11.5 | 62 | 5 | 4 |
7 | 23 | 299 | 8.6 | 65 | 5 | 7 |
8 | 19 | 99 | 13.8 | 59 | 5 | 8 |
9 | 8 | 19 | 20.1 | 61 | 5 | 9 |
12 | 16 | 256 | 9.7 | 69 | 5 | 12 |
13 | 11 | 290 | 9.2 | 66 | 5 | 13 |
14 | 14 | 274 | 10.9 | 68 | 5 | 14 |
15 | 18 | 65 | 13.2 | 58 | 5 | 15 |
16 | 14 | 334 | 11.5 | 64 | 5 | 16 |
17 | 34 | 307 | 12.0 | 66 | 5 | 17 |
18 | 6 | 78 | 18.4 | 57 | 5 | 18 |
19 | 30 | 322 | 11.5 | 68 | 5 | 19 |
20 | 11 | 44 | 9.7 | 62 | 5 | 20 |
21 | 1 | 8 | 9.7 | 59 | 5 | 21 |
22 | 11 | 320 | 16.6 | 73 | 5 | 22 |
23 | 4 | 25 | 9.7 | 61 | 5 | 23 |
24 | 32 | 92 | 12.0 | 61 | 5 | 24 |
28 | 23 | 13 | 12.0 | 67 | 5 | 28 |
29 | 45 | 252 | 14.9 | 81 | 5 | 29 |
30 | 115 | 223 | 5.7 | 79 | 5 | 30 |
31 | 37 | 279 | 7.4 | 76 | 5 | 31 |
38 | 29 | 127 | 9.7 | 82 | 6 | 7 |
40 | 71 | 291 | 13.8 | 90 | 6 | 9 |
41 | 39 | 323 | 11.5 | 87 | 6 | 10 |
44 | 23 | 148 | 8.0 | 82 | 6 | 13 |
47 | 21 | 191 | 14.9 | 77 | 6 | 16 |
48 | 37 | 284 | 20.7 | 72 | 6 | 17 |
... | ... | ... | ... | ... | ... | ... |
123 | 85 | 188 | 6.3 | 94 | 8 | 31 |
124 | 96 | 167 | 6.9 | 91 | 9 | 1 |
125 | 78 | 197 | 5.1 | 92 | 9 | 2 |
126 | 73 | 183 | 2.8 | 93 | 9 | 3 |
127 | 91 | 189 | 4.6 | 93 | 9 | 4 |
128 | 47 | 95 | 7.4 | 87 | 9 | 5 |
129 | 32 | 92 | 15.5 | 84 | 9 | 6 |
130 | 20 | 252 | 10.9 | 80 | 9 | 7 |
131 | 23 | 220 | 10.3 | 78 | 9 | 8 |
132 | 21 | 230 | 10.9 | 75 | 9 | 9 |
133 | 24 | 259 | 9.7 | 73 | 9 | 10 |
134 | 44 | 236 | 14.9 | 81 | 9 | 11 |
135 | 21 | 259 | 15.5 | 76 | 9 | 12 |
136 | 28 | 238 | 6.3 | 77 | 9 | 13 |
137 | 9 | 24 | 10.9 | 71 | 9 | 14 |
138 | 13 | 112 | 11.5 | 71 | 9 | 15 |
139 | 46 | 237 | 6.9 | 78 | 9 | 16 |
140 | 18 | 224 | 13.8 | 67 | 9 | 17 |
141 | 13 | 27 | 10.3 | 76 | 9 | 18 |
142 | 24 | 238 | 10.3 | 68 | 9 | 19 |
143 | 16 | 201 | 8.0 | 82 | 9 | 20 |
144 | 13 | 238 | 12.6 | 64 | 9 | 21 |
145 | 23 | 14 | 9.2 | 71 | 9 | 22 |
146 | 36 | 139 | 10.3 | 81 | 9 | 23 |
147 | 7 | 49 | 10.3 | 69 | 9 | 24 |
148 | 14 | 20 | 16.6 | 63 | 9 | 25 |
149 | 30 | 193 | 6.9 | 70 | 9 | 26 |
151 | 14 | 191 | 14.3 | 75 | 9 | 28 |
152 | 18 | 131 | 8.0 | 76 | 9 | 29 |
153 | 20 | 223 | 11.5 | 68 | 9 | 30 |
name=c('철수','춘향','길동')
age=c(22,20,25)
gender=factor(c('m','f','m'))
blood.type=factor(c('A','B','O'))
patient1 = data.frame(name,age,gender)
patient1
name | age | gender |
---|---|---|
철수 | 22 | m |
춘향 | 20 | f |
길동 | 25 | m |
patient2= data.frame(name,blood.type)
patient2
name | blood.type |
---|---|
철수 | A |
춘향 | B |
길동 | O |
patients = merge(patient1,patient2, by="name")
patients
name | age | gender | blood.type |
---|---|---|---|
길동 | 25 | m | O |
철수 | 22 | m | A |
춘향 | 20 | f | B |
x=array(1:12,c(3:4))
x
1 | 4 | 7 | 10 |
2 | 5 | 8 | 11 |
3 | 6 | 9 | 12 |
is.data.frame(x)
FALSE
x = as.data.frame(x) #데이터 프레임으로 변경
x
V1 | V2 | V3 | V4 |
---|---|---|---|
1 | 4 | 7 | 10 |
2 | 5 | 8 | 11 |
3 | 6 | 9 | 12 |
is.data.frame(x)
TRUE
names(x)=c("1st","2nd","3rd","4th") #열 이름 변경
x
1st | 2nd | 3rd | 4th |
---|---|---|---|
1 | 4 | 7 | 10 |
2 | 5 | 8 | 11 |
3 | 6 | 9 | 12 |
no.patients = data.frame(day=c(1:6),no=c(50,60,55,52,65,58))
no.patients
day | no |
---|---|
1 | 50 |
2 | 60 |
3 | 55 |
4 | 52 |
5 | 65 |
6 | 58 |
listpatients=list(patients=patients,no.patients=no.patients)
listpatients
- $patients
name age gender blood.type 길동 25 m O 철수 22 m A 춘향 20 f B - $no.patients
day no 1 50 2 60 3 55 4 52 5 65 6 58
listpatients$no.patients
day | no |
---|---|
1 | 50 |
2 | 60 |
3 | 55 |
4 | 52 |
5 | 65 |
6 | 58 |
listpatients$patients
name | age | gender | blood.type |
---|---|---|---|
길동 | 25 | m | O |
철수 | 22 | m | A |
춘향 | 20 | f | B |
listpatients[[1]]
name | age | gender | blood.type |
---|---|---|---|
길동 | 25 | m | O |
철수 | 22 | m | A |
춘향 | 20 | f | B |
listpatients[1]
$patients = <table>
</table>
listpatients[2]
$no.patients = <table>
</table>
listpatients[[2]]
day | no |
---|---|
1 | 50 |
2 | 60 |
3 | 55 |
4 | 52 |
5 | 65 |
6 | 58 |
listpatients[["no.patients"]]
day | no |
---|---|
1 | 50 |
2 | 60 |
3 | 55 |
4 | 52 |
5 | 65 |
6 | 58 |
listpatients["no.patients"]
$no.patients = <table>
</table>
listpatients[["patients"]]
name | age | gender | blood.type |
---|---|---|---|
길동 | 25 | m | O |
철수 | 22 | m | A |
춘향 | 20 | f | B |
listpatients["patients"]
$patients = <table>
</table>
##lapply 결과를 list 형태로 출력함
lapply(listpatients$no.patients,mean)
- $day
- 3.5
- $no
- 56.6666666666667
lapply(listpatients$patients, mean)
Warning message in mean.default(X[[i]], ...):
"argument is not numeric or logical: returning NA"Warning message in mean.default(X[[i]], ...):
"argument is not numeric or logical: returning NA"Warning message in mean.default(X[[i]], ...):
"argument is not numeric or logical: returning NA"
- $name
- <NA>
- $age
- 22.3333333333333
- $gender
- <NA>
- $blood.type
- <NA>
##sapply 결과를 벡터 결과로 출력함
x<-list(a=1:10)
x
lapply(x,mean)
$a = <ol class=list-inline>
</ol>
$a = 5.5
z<-sapply(x, mean)
z
is.vector(z)
is.vector(x)
a: 5.5
TRUE
TRUE