본문 바로가기
R/R을 이용한 데이터처리 분석 실무

데이터 타입

by java개발자 2016. 10. 4.

1.1 스칼라 - 숫자, NA, NULL, 문자열, 진릿값(TRUE, FALSE)

1.2 팩터 - 범주형(명목형, 순서형)

factor( )

nlevels( )

levels( )

is.factor( )

ordered( )

is.ordered( )

어떻게 사용?

2 벡터 - 한가지 데이터 타입, 중첩X, 벡터연산

c( )

names( )

names( ) <- 벡터

NROW( )

length( )

nrow( ) #행렬, 데프만가능


identical( )

union( )

intersect( )

setdiff( )

setequal( )


value %in% 벡터

x + 숫자             # +,*,/,-,==,!=


seq( )

seq_along( )

rep( ) - each, times

3 리스트 - 여러가지 데이터 타입, 중첩, 딕셔너리(키-값)

list(key=value, key=value)

x$key

x[n]        # 리스트 내의 키-값 서브리스트

x[[n]]

4.1 행렬 - 한가지 데이터 타입, 2차원

matrix(벡터, , , ,)

dimnames( )

dimnames( ) <- 리스트만?

rownames( )

rownames( ) <- 

colnames( )

colnames( )  <- 


A + n

A + B

A %*% B : 행렬곱

t( )         : 전치행렬

solve( )    : 역행렬

nrow( )

ncol( )

dim( )    : 차원

dim( ) <- 

4.2 배열 - 다차원

array(벡터,,,)

*배열중에 2차원은 행렬


5 데이터 프레임(데프) - 

data.frame( )

d$colname1

d$colname1 <- 6:10

str( )

d[, !names(d) %in% c("a")]

head( )

tail( )

View( )



6 타입 비교


명령어

결과

class(a)

str(a)

 설명

 

스칼라

(a1 = 2) [1] 2 [1] "numeric"  num 2  
(a2 = 1.1) [1] 1.1 [1] "numeric"  num 1.1  
(a3 = 'hello') [1] "hello" [1] "character"  chr "hello"  
(a4 = NA) [1] NA [1] "logical"  logi NA NA도 진릿값이네-_- 
(a5 = NULL) NULL [1] "NULL"  NULL  
(a6 = TRUE) [1] TRUE [1] "logical"  logi TRUE  
(a7 = FALSE) [1] FALSE [1] "logical"  logi FALSE  

 

1:5

[1] 1 2 3 4 5

[1] "integer"

 int [1:5] 1 2 3 4 5

 정수

 

 

seq(1:5)

[1] 1 2 3 4 5

[1] "integer"

 int [1:5] 1 2 3 4 5  

팩터

(a8 = factor("m", c("m","f"))) [1] m
Levels: m f
[1] "factor"  Factor w/ 2 levels "m","f": 1  
(a9 = ordered("m", c("m","f"))) [1] m
Levels: m < f
[1] "ordered" "factor"  Ord.factor w/ 2 levels "m"<"f": 1  
(a10 = ordered(1, c(1,2,3,4))) [1] 1
Levels: 1 < 2 < 3 < 4
[1] "ordered" "factor"  Ord.factor w/ 4 levels "1"<"2"<"3"<"4": 1  
(a11 = ordered(1, c("a",1,"b",2))) [1] 1
Levels: a < 1 < b < 2
[1] "ordered" "factor"  Ord.factor w/ 4 levels "a"<"1"<"b"<"2": 2  

벡터

(a12 = c(1,2,3)) [1] 1 2 3 [1] "numeric"  num [1:3] 1 2 3 벡터는 이름이 없나? 
(a13 = c("a","b","c","d")) [1] "a" "b" "c" "d" [1] "character"  chr [1:4] "a" "b" "c" "d"  
(a14 = c("a","b","c",1,2,3)) [1] "a" "b" "c" "1" "2" "3" [1] "character"  chr [1:6] "a" "b" "c" "1" "2" "3"  

 

c(1:4)

[1] 1 2 3 4

[1] "integer"

 int [1:4] 1 2 3 4

  

리스트

(a15 = list(name="foo", height=c(1,3,4))) $name
[1] "foo"
$height
[1] 1 3 4
[1] "list" List of 2
 $ name  : chr "foo"
 $ height: num [1:3] 1 3 4
  

행렬

(a16 = matrix(1:9, nrow=3))      [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
[1] "matrix"  num [1:3, 1:3] 1 2 3 4 5 6 7 8 9  
(a17 = matrix(c("a","b","c","d"), ncol=2))      [,1] [,2]
[1,] "a"  "c"
[2,] "b"  "d" 
[1] "matrix"  chr [1:2, 1:2] "a" "b" "c" "d"  

배열

(a18 = 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
[1] "array"  int [1:2, 1:2, 1:3] 1 2 3 4 5 6 7 8 9 10 …

 1:12 또는

 seq(1:12) 로 만들면

 num 이 아니라 int(정수) 다.

 
 (a22 = array(c("a","b","c","d"))) [1] "a" "b" "c" "d" [1] "array" chr [1:4(1d)] "a" "b" "c" "d"  
 (a23 = array(c("a","b","c","d"),dim=c(2,2)))      [,1] [,2]
[1,]   "a"  "c" 
[2,]   "b"  "d" 
 [1] "matrix" chr [1:2, 1:2] "a" "b" "c" "d"

 array로 만들어도 matrix 구나.ㅜㅜ

 

데이터
프레임

(a19 = data.frame(x=1:2, y=3:4))     x  y
1   1 3
2   2 4
[1] "data.frame" data.frame': 12 obs. of  2 variables:
 $ x: int  1 2 3 4 5 6 7 8 9 10 ...
 $ y: int  13 14 15 16 17 18 19 20 21 22 …
  
(a20 <- data.frame(x=c(1, 2, 3, 4, 5)
,y=c(2, 4, 6, 8, 10)
,z=c('M', 'F', 'M', 'F', 'M')))
  x  y z
1 1  2 M
2 2  4 F
3 3  6 M
4 4  8 F
5 5 10 M
[1] "data.frame" data.frame': 5 obs. of  3 variables:
 $ x: num  1 2 3 4 5
 $ y: num  2 4 6 8 10
 $ z: Factor w/ 2 levels "F","M": 2 1 2 1 2
  
(a21 <- data.frame(x=c(1, 2, 3, 4, 5)
,y=c(2, 4, 6, 8, 10)
,z=c('M', 'F', 'M', 'F', 'M')
, stringsAsFactors=FALSE))
  x  y z
1 1  2 M
2 2  4 F
3 3  6 M
4 4  8 F
5 5 10 M
[1] "data.frame" data.frame': 5 obs. of  3 variables:
 $ x: num  1 2 3 4 5
 $ y: num  2 4 6 8 10
 $ z: chr  "M" "F" "M" "F" …

 stringsAsFactors 을 사용한 경우 Factor가 아니다.

 




'R > R을 이용한 데이터처리 분석 실무' 카테고리의 다른 글

apply 계열 함수  (0) 2016.10.31
R 리스트  (0) 2016.10.31