Sử dụng vòng lặp for với R

Tác giả: Nguyễn Hải Trường | 2019-07-30

Trong quá trình phân tích dữ liệu thực tế, chúng ta sẽ có thể phải sử dụng vòng lặp (loop) đối với những bước tính toán hoặc thao tác mà được lặp đi lặp lại nhiều lần. Trong bài viết này, RAnalytics sẽ chia sẻ với các bạn cách sử dụng vòng lặp for trong R.

Cấu trúc của vòng lặp for như sau:

for (value in sequence) {

statement

}

Ví dụ 1: Vòng lặp for đơn giản

for(i in 2010:2019) {
  output <- paste("The year is", i)
  print(output)
}
## [1] "The year is 2010"
## [1] "The year is 2011"
## [1] "The year is 2012"
## [1] "The year is 2013"
## [1] "The year is 2014"
## [1] "The year is 2015"
## [1] "The year is 2016"
## [1] "The year is 2017"
## [1] "The year is 2018"
## [1] "The year is 2019"

Ví dụ 2:

Sử dụng vòng lặp thực hiện so sánh ANOVA lần lượt Sepal.Length, Sepal.Width, Petal.Length, Petal.Width giữa các loài hoa (Species)

library(dplyr)
for (i in c(1:4)){
  df <- iris %>% select(i, Species)
  # Hiện thị công thức mô tả mối quan hệ giữa 2 biến
  formula.aov <- paste(names(df)[1], names(df)[2], sep = "~") 
  print(formula.aov)
  aov(formula(formula.aov), df) %>% TukeyHSD %>% print # ANOVA
}
## [1] "Sepal.Length~Species"
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = formula(formula.aov), data = df)
## 
## $Species
##                       diff       lwr       upr p adj
## versicolor-setosa    0.930 0.6862273 1.1737727     0
## virginica-setosa     1.582 1.3382273 1.8257727     0
## virginica-versicolor 0.652 0.4082273 0.8957727     0
## 
## [1] "Sepal.Width~Species"
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = formula(formula.aov), data = df)
## 
## $Species
##                        diff         lwr        upr     p adj
## versicolor-setosa    -0.658 -0.81885528 -0.4971447 0.0000000
## virginica-setosa     -0.454 -0.61485528 -0.2931447 0.0000000
## virginica-versicolor  0.204  0.04314472  0.3648553 0.0087802
## 
## [1] "Petal.Length~Species"
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = formula(formula.aov), data = df)
## 
## $Species
##                       diff     lwr     upr p adj
## versicolor-setosa    2.798 2.59422 3.00178     0
## virginica-setosa     4.090 3.88622 4.29378     0
## virginica-versicolor 1.292 1.08822 1.49578     0
## 
## [1] "Petal.Width~Species"
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = formula(formula.aov), data = df)
## 
## $Species
##                      diff       lwr       upr p adj
## versicolor-setosa    1.08 0.9830903 1.1769097     0
## virginica-setosa     1.78 1.6830903 1.8769097     0
## virginica-versicolor 0.70 0.6030903 0.7969097     0

Như vậy, chúng ta đã vừa được học cách sử dụng vòng lặp for với R. Chúc các bạn học tập và làm việc hiệu quả với RAnalytics.vn!

comments powered by Disqus