• R
    • 소개 http://www.r­project.org/
    • 1976년 벨 연구소에서 S language 등장(John Chambers)
    • 1993년 오클랜드 대학교에서 R language 개발(Ross Ihaka, Robert Gentleman) 오픈소스 프로그래밍 언어, 공짜
    • 현재 가장 많은 통계학자와 분석가가 사용, 5000개 이상의 package 등록
    • R은 만든 사람 이름이 R로 시작해서... 어이가 없다 
    • SAS보다 많이 씀. 가장 많은 통계학자와 분석가가 사용 중
    • 14년 기준 Package가 5,000개가 넘는다.  (Dataset과 Function의 모음이 Package가 됨) R의 경쟁력 원천임
    • R도 Language인것은 맞음. Interpreter계열로 실행 속도는 느리지만 작업 속도는 더 빠름 (대화식.)
    • R Studio는 지원 Tool로서 R자체를 직접 실행시킬 일이 없음 
    • Shiny Web Application: Data Visualization 
    • 존재하는 모든 것은 object 이며 모든 동사는 function call
  • Vector는 여러개의 값으로 이루어진 하나의 object 
    • '사번 - 이름 - 성별 - 나이'는 하나의 Vector로서 4개의 원소를 갖는다
    • 행 Vector와 열 Vector이 있음 
  • type of object

    “To understand computations in R, two slogans are helpful:

    ● Everything that exists is an object.

    ● Everything that happens is a function call.”

    — John Chambers

    object type을 따지는 이유는 type에 따라서 쓸 수 있는 function이 다르기 때문

    ‘a’ + ‘b    vs. ’1+2

  • atomic vector

가장 기본이 되는 object type

데이터 분석에서 vector는 연산의 기본 단위.

1:10

c(1,2,3,4,5,6,7,8,9,10) #concatenate is.vector(1:10)

length(1:10)

중요!: 하나의 벡터는 오직 하나의 데이터 타입(type of data)만 저장할 수 있다.

  • type of object
  • 하나의 벡터는 오직 하나의 데이터 타입(type of data)만 저장할 수 있다.
  • types of data

    • 6가지: double, integer, character, logical, complex, raw

  • factor

    • 범주형 자료(categorical data): 성별, 학점 등


# check working directory

getwd()


# print object

print(1)

print

print(x)


# Arithmetic Operators

+10

-7

1 + 1

10 - 3

7 * 6

4 / 7

2^3

8 %% 2

8 %/% 2

8


# object

'a'

a


# assignment

a <- 1

a + 1    

rm(a)                   # remove 

ls()                       # lits objects 


# element-wise execution

v10 <- 1:10         # assign number

v10 + 1               # increase each cell 

v10 / 2                # divide each cell

v10 * 2

v10 + v10

v10 * v10

v10^2


# vector recycling

v2 <- 1:2 

v10 + v2              # add 1,2 repeatedly 

rep(v2, 5)            # repeat v2 5times 

v10 <- rep(v2,5)

v10

v3 <- 1:3              # assign 1,2,3 

v10 + v3              # error 

rep(v3, 4)[1:10]   # limit to 10

rep(v3, 4)            # up to 12 



# vector: same type  
1:10
c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
c(1:10)
v.10 <- 1:10
v.10  <- c(1:10) 
v.str <- c('one', 'two', 'three')
v.str

length(v.10)  # show length

typeof(v.10)  # show type 
typeof(v.str)

v.diff <- c(1, 'one')
v.diff                  # force type as character 

# double
typeof(1)            # double is default 
pi
typeof(pi)         # double, floating 

# integer
typeof(1) 
typeof(1L)    # integer
typeof(1:10)      # vector must be used with same type 

# character
a <- "fufu"
a
typeof(a)

typeof(c('one', 'two')) 
typeof('one','two')  

'1'       #character
1         #number 
'1' + 1   #error

# logical
1 > 2
2 > 1
typeof(1 > 2) # dont' care T or F 
v.logic <- c(TRUE, FALSE)
v.logic
typeof(v.logic)

# Attributes
v.10 <- 1:10
v.10
attributes(v.10)    #null, Object Attribute Lists
attributes (pi)     #null
dim(v.10)           #null
v.10
dim(v.10) <- c(2, 5)  #Retrieve or set the dimension of an object.
v.10
attributes(v.10)
typeof(v.10)      # integer, show type of cell
class(v.10)       # matrix, show Object Classes
class(1:10)       # integer,show Object Classes

# factor, show distinguished factor
v.10 <- 1:10
f.10 <- factor(v.10)
factor(v.10)
f.10
class(f.10)       #factor, show object classes
attributes(f.10)  #factor 
typeof(f.10)      #integer
letters[1:10]     #show 1~10th of alphabet
vv <- c(1,3,2,1,1,2)
vv
factor(vv)
vc <- c('a','b','b','c','D','d')
factor(vc)        # d,D distinguished

grades <- c('A', 'B', 'C', 'A', 'A+', 'B-')
grades
typeof(grades)
f.grades <- factor(grades)
f.grades
class(f.grades)
uf.grades <- unclass(f.grades)  #뭔소리인지 모르겠다. 
uf.grades           # integer
class(uf.gradeses)
typeof(uf.grades)
attributes(uf.grades)
attributes(uf.grades) <- NULL
is.vector(uf.grades)

# indexing
v.10 <- 1:10
v.10[1]
v.10[2:4]
dim(v.10) <- c(2, 10)   #error

v.10 <- 1:20
dim(v.10) <- c(2, 10)   #done

v.10[1, 2]
v.10[1, 3:4]    
v.10[-1, c(2, 5)]   #same
v.10[2, c(2, 5)]    #same

v.10[1, ]
v.10[, 2]



 

R 시작을 위한 기본 지식  








Posted by Name_null