반응형

source: R을 이용한 데이터 처리 & 분석 실무, 서민구 지음

http://book.naver.com/bookdb/book_detail.nhn?bid=8317471


1. 스칼라: 단일 차원의 값을 뜻함. 1,2,3

1) NA: Not available라는 상수. 값이 없음, 결측값. 값이 빠진 경우를 뜻함.

> four<-NA

> is.na(four)

[1] TRUE

 

2) NULL: NA와는 다른 개념으로 undefined , 미정값.

> x<-NULL

> is.null(x)

[1] TRUE

> is.null(1)

[1] FALSE

> is.null(NA)

[1] FALSE

> is.na(NULL)

logical(0)

Warning message:

In is.na(NULL) : is.na() applied to non-(list or vector) of type 'NULL'

 

3) 진리값: T/F 사용

> T&F

[1] FALSE

> T|T

[1] TRUE

> T|F

[1] TRUE

> T<-TRUE

> T

 [1] TRUE

> T<-FALSE

> T

[1] FALSE

> TRUE<-FALSE

Error in TRUE <- FALSE : invalid (do_set) left-hand side to assignment

 

잘 이해안되는 부분..좀 더 공부 필요 한 부분.

 

> c(TRUE,TRUE)&c(TRUE,FALSE)

[1]  TRUE FALSE

> c(TRUE,TRUE)&&c(TRUE,FALSE)

[1] TRUE

 

 

4) 팩터: 범주형 (categorical) 데이터 자료를 표현하기 위한 데이터 타입. //

 - 명목형(Nominal): /

 - 순서형(Ordinal): //

 

> sex<-factor("m",c("m","f")) #sex에는 "m" 지정, 팩터의 레벨은 "m","f" 제한> sex

[1] m

Levels: m f

> nlevels(sex)

[1] 2

> levels(sex)

[1] "m" "f"

> levels(sex)[1]

[1] "m"

> levels(sex)[2]

[1] "f"

> levels(sex)<-c("male","female")

> sex

[1] male

Levels: male female

> factor(c("m","m","f"),c("m","f"))

[1] m m f

Levels: m f

> factor(c("m","m","f"))

[1] m m f

Levels: f m

> ordered("a",c("a","b","c"))

[1] a

Levels: a < b < c

 

2. 벡터(Vector): 배열의 개념. 가지 스칼라 데이터 타입의 데이터를 저장

) 숫자만 저장 또는 문자열만 저장하는 배열이 벡터에 해당.

> x<-c(1,3,4)

> names(x)<-c("a","b","c")

> x

a b c

1 3 4

> x[1]

a

1

> x[-1]

b c

3 4

> x[c(1,3)]

a c

1 4

> x["a"]

a

1

> x[c("b","c")]

b c

3 4

> names(x)[2]

[1] "b"

> length(x)

[1] 3

> nrow(x)

NULL

> NROW(x)

[1] 3

> identical(c(1,2,3),c(1,2,3)) #객체가 동일한지 판단

[1] TRUE

> identical(c(1,2,3),c(1,2,1))

[1] FALSE

> "a"%in%c("a","b","c")

[1] TRUE

> "d"%in%c("a","b","c")

[1] FALSE

> x<-c(1:5)

> x

[1] 1 2 3 4 5

> x+1

[1] 2 3 4 5 6

> 10-x

[1] 9 8 7 6 5

> c(1,2,3)==c(1,2,3)

[1] TRUE TRUE TRUE

> union(c("a","b"),"d")

[1] "a" "b" "d"

> intersect(c("a","b","c"),c("a","d"))

[1] "a"

> setequal(c("a","b","c"),c("a","d"))

[1] FALSE

> setequal(c("a","b"),c("a","b","b"))

[1] TRUE

> setequal(c("a","b"),c("a","b","c"))

[1] FALSE

 

> seq(3,7)

[1] 3 4 5 6 7

> seq(7,3)

[1] 7 6 5 4 3

> seq(3,7,2)

[1] 3 5 7

> seq(3,7,3)

[1] 3 6

> x<-seq(2,10,2)

> x

[1]  2  4  6  8 10

> 1:NROW(x)

[1] 1 2 3 4 5

> seq_along(x)

[1] 1 2 3 4 5

> rep(1:2,times=5)

 [1] 1 2 1 2 1 2 1 2 1 2

> rep(1:2,each=5)

 [1] 1 1 1 1 1 2 2 2 2 2

> rep(1:2,each=5,times=2)

 [1] 1 1 1 1 1 2 2 2 2 2 1 1 1 1 1 2 2 2 2 2

> (x<-list(name="foo",height=70))

$name

[1] "foo"

 

$height

[1] 70

 

3. list: 벡터와 달리 서로 다른 값을 담을 있음.

> (x<-list(names="foo",height=c(1,3,5)))

$names

[1] "foo"

 

$height

[1] 1 3 5

 

> list(a=list(val=c(1,2,3)),b=list(val=c(1,2,3,4)))

$a

$a$val

[1] 1 2 3

 

 

$b

$b$val

[1] 1 2 3 4

 

 

> x$name

[1] "foo"

> x

$names

[1] "foo"

 

$height

[1] 1 3 5

 

> x$names

[1] "foo"

> x$height

[1] 1 3 5

> x[1]

$names

[1] "foo"

 

> x[[1]]

[1] "foo"

 

4. matrix: 행렬

> matrix(c(1:9),nrow=3,byrow=T)

     [,1] [,2] [,3]

[1,]    1    4    7

[2,]    2    5    8

[3,]    3    6    9

> matrix(c(1:9),nrow=3)

     [,1] [,2] [,3]

[1,]    1    4    7

[2,]    2    5    8

[3,]    3    6    9

> matrix(c(1:9),nrow=3,

+        dimnames=list(c("r1","r2","r3"),c("c1","c2","c3")))

   c1 c2 c3

r1  1  4  7

r2  2  5  8

r3  3  6  9

> (x<-matrix(c(1:9),nrow=3))

     [,1] [,2] [,3]

[1,]    1    4    7

[2,]    2    5    8

[3,]    3    6    9

> dimnames(x)<-list(c("r4","r5","r6"),c("c4","c5","c6"))

> x

   c4 c5 c6

r4  1  4  7

r5  2  5  8

r6  3  6  9

>

> x<-matrix(1:9,ncol=3)

> x

     [,1] [,2] [,3]

[1,]    1    4    7

[2,]    2    5    8

[3,]    3    6    9

> rownames(x)<-c("r1","r2","r3")

> x

   [,1] [,2] [,3]

r1    1    4    7

r2    2    5    8

r3    3    6    9

> colnames(x)<-c("c1","c2","c3")

> x

   c1 c2 c3

r1  1  4  7

r2  2  5  8

r3  3  6  9

> x[1,1]

[1] 1

> x[,2]

r1 r2 r3

 4  5  6

> x[1:2,]

   c1 c2 c3

r1  1  4  7

r2  2  5  8

> x[,1:2]

   c1 c2

r1  1  4

r2  2  5

r3  3  6

> x[c(1,2),c(2,1)]

   c2 c1

r1  4  1

r2  5  2

> x[c(1,1),c(1,3)]

   c1 c3

r1  1  7

r1  1  7

> x[c(1,3),c(1,3)]

   c1 c3

r1  1  7

r3  3  9

> x[c(2,1),c(1,2)]

   c1 c2

r2  2  5

r1  1  4

> x<-matrix(1:9,nrow=3)

> x*2

     [,1] [,2] [,3]

[1,]    2    8   14

[2,]    4   10   16

[3,]    6   12   18

> x+x

     [,1] [,2] [,3]

[1,]    2    8   14

[2,]    4   10   16

[3,]    6   12   18

> x%*%x

     [,1] [,2] [,3]

[1,]   30   66  102

[2,]   36   81  126

[3,]   42   96  150

> t(x)

     [,1] [,2] [,3]

[1,]    1    2    3

[2,]    4    5    6

[3,]    7    8    9

> (x<-matrix(c(1:4),ncol=2))

     [,1] [,2]

[1,]    1    3

[2,]    2    4

> solve(x)

     [,1] [,2]

[1,]   -2  1.5

[2,]    1 -0.5

> (x<-matrix(c(1:6),nrow=2))

     [,1] [,2] [,3]

[1,]    1    3    5

[2,]    2    4    6

> dim(x)

[1] 2 3

> dim(x)<-c(3,2)

> x

     [,1] [,2]

[1,]    1    4

[2,]    2    5

[3,]    3    6

  

5. Array: 배열. Matrix행렬이 2차원이라면 배열은 다차원 데이터

> array(1:12,dim=c(3,4))

     [,1] [,2] [,3] [,4]

[1,]    1    4    7   10

[2,]    2    5    8   11

[3,]    3    6    9   12

> (x<-array(1:12,dim=c(2,2,3)))

, , 1

 

     [,1] [,2]

[1,]    1    3

[2,]    2    4

 

, , 2

 

     [,1] [,2]

[1,]    5    7

[2,]    6    8

 

, , 3

 

     [,1] [,2]

[1,]    9   11

[2,]   10   12

 

> x[1,1,3]

[1] 9

반응형
Posted by 마르띤
,