728x90
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)
728x90
'Language > R' 카테고리의 다른 글
R에서 나는 에러들(invalid multibyte character, Error in Select, 한글깨짐) (1) | 2020.08.12 |
---|---|
02 R 자주 쓰는 기능(read.csv, merge, distinct, function import, factor, group by) (0) | 2020.07.12 |
01 R Hello World 출력하기(Vector, DataFrame, filter()) (0) | 2020.06.23 |