R 프로그래밍

빅데이타

R 프로그래밍

1 미지 0 5904 1 0

R Programming

[값 대입 : 대입 연산자]

x = 10  # = 가능한 사용 X (동작 안 하는 경우 있음)
x <- 20 # <- 올바른 사용
c <- c("a","b","c")

[변수 리스트 보기]

ls()

[특정 변수 삭제 (메모리 해제)]

rm(x)

[모든 변수 삭제]

rm(list=ls())

[변수 타입 확인]

mode(3.1415)
[1] "numeric"

[C : Combine]

C(1,2,3)

[값 출력]

1:5
[1] 1 2 3 4 5

9:0
[1] 9 8 7 6 5 4 3 2 1 0

[seq: FOR문과 유사한 반복문]

seq(from=1, to=5, by=2)
[1] 1 3 5

s <- as.Date("2015-01-01")
e <- as.Date("2015-01-15")
seq(s,e,1)
[1] "2015-01-01" "2015-01-02" "2015-01-03" "2015-01-04" "2015-01-05" "2015-01-06" "2015-01-07"
[8] "2015-01-08" "2015-01-09" "2015-01-10" "2015-01-11" "2015-01-12" "2015-01-13" "2015-01-14"
[15] "2015-01-15"
seq(from=s,by=1,length.out=7)
[1] "2015-01-01" "2015-01-02" "2015-01-03" "2015-01-04" "2015-01-05" "2015-01-06" "2015-01-07"

[rep: 특정 회수 만큼 반복하는 반복문]

rep(1, times=5)
[1] 1 1 1 1 1

[as.Date : 날짜 함수]

e <- as.Date("2015-01-15")

[상수]

LETTERS: the 26 upper-case letters of the Roman alphabet;
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W"
[24] "X" "Y" "Z"

letters: the 26 lower-case letters of the Roman alphabet;
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w"
[24] "x" "y" "z"

month.abb: the three-letter abbreviations for the English month names;
[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"

month.name: the English names for the months of the year;
[1] "January"   "February"  "March"     "April"     "May"       "June"      "July"     
[8] "August"    "September" "October"   "November"  "December" 

pi: the ratio of the circumference of a circle to its diameter.
[1] 3.141593

[부등호]

v <- c(3,pi,4)
w <- c(pi,pi,pi)
v == w
[1] FALSE TRUE FALSE
> v != w
[1] TRUE FALSE TRUE
> v < w
[1] TRUE FALSE FALSE
> v > w
[1] FALSE FALSE TRUE
> v <= w
[1] TRUE TRUE FALSE
> v >= w
[1] FALSE TRUE TRUE

[any: OR 조건]

v <- c(3,pi,4)
any(v == pi)
[1] TRUE

[all: AND 조건]

v <- c(3,pi,4)
all(v == pi)
[1] FALSE

[names: ]

years <- c(1960,1964,1976,1994)
names(years) <- c("Kenndey","Johnson","Carter","Clinton")

years
Kenndey Johnson  Carter Clinton 
   1960    1964    1976    1994
years["Carter"]
Carter
1976

years[c("Carter","Clinton")]
Carter Clinton
1976 1994

※ 2개 이상 쓸 때는 c() COMBINE 을 활용 해야 함.

[gegwd: 작업디렉토리 확인]

getwd( )

[setwd : 작업디렉토리 변경]

일시 변경
setwd("Rtest")

영구 변경
Rstudio에서 setwd( )
Session > Set Working Directory > Choose Directory

[length : ]

x<-c("a","b","c")
length(x)
[1] 3

[nrow : 행렬 길이 반환] 행렬만 가능

nrow(x)
NULL

[NROW : 벡터, 행렬 길이 반환] 벡터와 행렬 모두 가능

NROW(X)
[1] 3

연산자

[identical : 객체가 동일한지 판단]

identical(c(1,2,3),c(1,2,3))
[1] TRUE

identical(c(1,2,3),c(1,2,100))
[1] FALSE

[%in% : 값이 포함 되어있는지 체크]

"a" %in% c("a","b","c")
[1] TRUE

"d" %in% c("a","b","c")
[1] FALSE

[ + : 각 요소별로 연산]

x<-c(1,2,4,7,3)
x+1
[1] 2 3 5 8 4

[union : 합집합]

union(c("a","b","c"),c("a","d"))
[1] "a" "b" "c" "d"

[intersect : 교집합]

intersect(c("a","b","c"),c("a","d"))
[1] "a"

[setdiff: 차집합] 앞의 요소에서 뒤의 요소를 뺌. 앞의 요소에만 값 남기는것

setdiff(c("a","b","c"),c("a","d"))
[1] "b" "c"

[setequal : 같은 집합인지 판단]

setequal(c("a","b","c"),c("a","d"))
[1] TRUE

setequal(c("a","b","c"),c("a","b","c","c"))
[1] FALSE

[return 결과값의 의미]

NA : 값이 없음
NULL : 정해지지 않은 값

0 Comments
포토 제목
Category
Facebook Twitter GooglePlus KakaoStory KakaoTalk NaverBand