반응형

/*********************************************************************************************************
-- Title : [R3.3] 변수 및 데이터 형식, 데이터형 변환
-- Reference : hrd-net, blog.naver.com/easternsun
-- Key word : 
벡터 vector 펙터 factor mode class str ls Date time timezone getlocale setlocale 로케일 locale 자료형 데이터타입 
                  데이터 타입 data type
*********************************************************************************************************/

-- R 데이터 기본 구조


-- R

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
  
 
# ********************************************
# -- 변수
#    scalr vs. vector
#    ls() : 변수 목록 보기 
# ********************************************
 
# --일반 변수
var1 = 10
var1
 
var2=20
var3=30
var2;var3
 
# -- 변수.멤버
member.id = 10
member.age = 43
 
member.id + member.age
 
# -- scalar vs. vector
#    scalar : 1개 값 저장
#    vector : 2개 이상 값 저장, Pytion의 리스트와 비슷
#             단, 2개 이상 형식이 있을 때는 하나의 형식으로 자동 형변환
age = 35
name = '홍길동'
age; name
 
age2 = c(35,24,26)
name2 = c('aaa','bbb','ccc')
 
age2;age2[1]
name2;name2[2]
 
ttt = c(35'ttt')
ttt
 
# -- 변수 목록 보기
ls()
 
 
# ********************************************
# -- 데이터 형
#    mode()        : 데이터 형 확인
#    is.numeric()  : 숫자인지 확인
#    is.na, is.nan : NULL인지 확인  
# ********************************************
aaa = 10                  # numeric
bbb = "ggg"               # character
ccc = TRUE                # boolean
 
aaa;bbb;ccc
 
# -- 변수 타입 리턴
mode(aaa)
mode(bbb)
mode(ccc)
is.numeric(aaa)
is.numeric(bbb)
 
# -- 결측치(NULL)
score = c(1,2,NA, 3,4, NaN)
score1 = c(1,2,NaN, 3,NA)
score;score1
 
mean(score)               # 계산되지 않음
mean(score, na.rm=TRUE)   # NA 제외
 
sum(score1)               # 계산되지 않음
sum(score1, na.rm=T)      # NA 제외 
 
is.na(score)              # NA & NaN 모두 추출
is.nan(score1)            # NaN인것만 추출 
 
 
# ********************************************
# -- 데이터 형 변환
#    as.numeric()    : 숫자로 변환
#    as.factor()     : 요인형으로 변환
#    strptime        : 문자 -> 일자형으로 변환 
#    mode()          : 데이터 형식 확인 
#    class()         : 객체 유형 확인 
#    str()           : 객체 구조 확인
#    Sys.getlocale() : 로케일 정보 확인
#    Sys.setlocale() : 로케일 정보 변
# ********************************************
 
# -- 문자 -> 숫자
= c(1,2,3,'40')         # vector는 하나의 형식으로 자동 변환됨
x
* 0.5                       # 에러... 문자열로 변환 되어서
 
num = as.numeric(x)
num ** 0.5                    # sqrt
 
hist(x)                       # error : 'x'는 반드시 숫자이어야 합니다
hist(num)
 
 
# -- 요인형(Factor) 변환 : 동일한 값을 범주로 갖는 벡터형의 수량화 
gender = c('M','F','M','M','F')
gender
 
plot(gender)                  #error : 그래프는 숫자로만 가능
 
fgender = as.factor(gender)   # 문자열 -> 요인형으로 변환
fgender
 
plot(fgender)
str(fgender)
 
# -- 더미변수 
dd = c(1,0,1,0,1,1,0,1,0,1,1,1,0,1,0)
ddd = as.factor(dd)
 
plot(ddd)
 
# -- mode() vs. class() vs. str()
mode(fgender)
class(fgender)
str(fgender)
 
# -- 날짜형
Sys.Date()
Sys.time()
Sys.timezone()
 
# -- 문자열 -> 날짜형
today = '2016-11-05 11:56:58'
today
 
mode(today)
class(today)
str(today)
 
ctoday = strptime(today, '%Y-%m-%d %H:%M:%S')
ctoday
mode(ctoday)
class(ctoday)
 
# -- 영문식 -> 한국식
sdate = '13-Jun-16'
 
Sys.getlocale()                                  # 한국어로 다국어가 되어 있음(LC_COLLATE=Korean_Korea.949)
 
Sys.setlocale(locale='English_USA')    # LC_COLLATE=English_United States.1252
 
etoday = strptime(sdate, '%d-%b-%y')  # 축약된 월:%b, 전체 월:%B
etoday
 
Sys.setlocale(locale='Korean_Korea')
Sys.getlocale()
 
 
 
 

cs

반응형

+ Recent posts