본문 바로가기

Contact English

【RStudio】 3강. 배열

 

3강. 배열(array)

 

추천글 : 【RStudio】 R 스튜디오 목차 


1. 배열 자료형 연산 [본문]

2. 배열의 선언 [본문]

3. 배열 관련 유용한 툴 [본문]


 

1. 배열 자료형 연산 [목차]

 

1:10 
# [1] 1 2 3 4 5 6 7 8 9 10 
0:-10 
# [1] 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 
0.1:10 
# [1] 0.1 1.1 2.1 3.1 4.1 5.1 6.1 7.1 8.1 9.1 
seq(5) 
# [1] 1 2 3 4 5 
seq(1, 4, 0.1) 
# [1] 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 
# [19] 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0 
seq(1, 4, length = 5) # length is the number of elements 
# [1] 1.00 1.75 2.50 3.25 4.00 
rep("b", 3) 
# [1] "b" "b" "b" 
c(0, 3)
# [1] 0 3
rep(c(0, 3), 2) 
# [1] 0 3 0 3 
rep(1:4, 3) 
# [1] 1 2 3 4 1 2 3 4 1 2 3 4 
rep(c(1, 2, 3), c(1, 2, 3)) 
# [1] 1 2 2 3 3 3 
rep(c(1, 2, 3), each = 4) 
# [1] 1 1 1 1 2 2 2 2 3 3 3 3 
rep(c(1, 2, 3), length = 10) # length is the number of elements 
# [1] 1 2 3 1 2 3 1 2 3 1 
x <- seq(1, 12, 2) 
x+1 
# [1] 2 4 6 8 10 12 
x*10 
# [1] 10 30 50 70 90 110 
x %/%2 
# [1] 0 1 2 3 4 5 
x %% 2 
# [1] 1 1 1 1 1 1 
x > 2 
# [1] FALSE TRUE TRUE TRUE TRUE TRUE 
length(x)
# [1] 6 
cumsum(x) 
# [1] 1 4 9 16 25 36 
rev(x) 
# [1] 11 9 7 5 3 1 
mean(x) # average 
# [1] 6 
sd(x) # standard deviation 
# [1] 3.741657 
sum(x) # summation 
# [1] 36 
min(x) 
# [1] 1 
max(x) 
# [1] 11 
y <- c(4, 4.5, 6) 
x + y 
# [1] 5.0 7.5 11.0 11.0 13.5 17.0 
x * y 
# [1] 4.0 13.5 30.0 28.0 40.5 66.0 
cor(x[1:3], y) 
# 0.9607689

 

 

2. 배열의 선언 [목차]

 

u <- array() # generate a vector in any size 
v <- array(dim = 2) # generate a vector of size 2 
w <- numeric(10000) # generate a vector of size 10000 
sample(1:100, size = 10000, replace = T) # random sampling with replacement

 

 

 

3. 배열 관련 유용한 툴 [목차]

 

x <- seq(1, 12, 2) 
summary(x) # min, 1st quartile, median, mean, 3rd quartile, max 
# Min. 1st Qu. Median Mean 3rd Qu. Max. 
# 1.0 3.5 6.0 6.0 8.5 11.0 
quantile(x) 
# 0% 25% 50% 75% 100% 
# 1.0 3.5 6.0 8.5 11.0 
quantile(x, seq(0, 1, 0.1)) 
# 0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100% 
# 1 2 3 4 5 6 7 8 9 10 11 
range(x) 
# [1] 1 11 
prod(x) 
# [1] 10395 
grep(11, x) # the location of "11" in x 
# [1] 6

 

입력 : 2019.09.23 23:35