티스토리 뷰

r을 실행 한다.

자주 사용하는 단축키

설명 단축키
새탭 command + shift + N
커서 있는 라인만 실행 command + Enter
전체 실행 command + shift + S
주석 command + shift + c

hello 출력 하기

print('hello')

결과

프로그램 번호 없이 출력하기

message('world')

결과

벡터(Vector) 만들기
Vector는 일종의 list다.

c('hello', 'world')

결과
[1] "hello" "world"

벡터 만들고 출력하기

vector1 <- c('hello', 'world')
message(vector1)
print(vector1)

결과
> message(vector1)
helloworld
> print(vector1)
[1] "hello" "world"

벡터의 연산

v1 <- c(1, 2, 3)
v2 <- c(2, 3, 4)
v1+v2

결과
[1] 3 5 7

벡터를 매트릭스로 만들기

매트릭스는 모든 데이터가 같은 type으로 되어 있다.

v1 <- 1:4
mat <- matrix(v1)
mat

결과

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

매트릭스는 컬럼을 기준으로 값이 들어간다.

DataFrame만들기

# Make DataFrame
v1 <- c(1, 2, 3, 4)
v2 <- c('Apple', 'Peach', 'Banana', 'Grape')
v3 <- c(500, 200, 100, 50)
v4 <- c(5, 2, 4, 7)

sales <- data.frame(v1, v2, v3, v4)

결과

  v1     v2  v3 v4
1  1  Apple 500  5
2  2  Peach 200  2
3  3 Banana 100  4
4  4  Grape  50  7

위 sales에서 1~2행까지 v1, v2컬럼만 뽑기

sales[1:2, c("v1",'v2')]

결과

  v1    v2
1  1 Apple
2  2 Peach

조건 2개로 필터링 하기

# install.packages('dplyr') # data processing library
library(dplyr)

# df만들기 id, class 두가지만 만들었음
df <- data.frame(id = c(1:6), class = c('1', '1', '1', '1', '2', '2'))

df_filtered <- df %>% filter(class == 1 & id >= 3)

print(df_filtered)
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
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
글 보관함