A10 - Base R

A10 - Base R#


Revised

20 Jun 2023


Programming Environment#

utils::sessionInfo()
R version 4.3.0 (2023-04-21)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS 15.1

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/New_York
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] digest_0.6.31   IRdisplay_1.1   utf8_1.2.3      base64enc_0.1-3
 [5] fastmap_1.1.1   glue_1.6.2      htmltools_0.5.5 repr_1.1.6     
 [9] lifecycle_1.0.3 cli_3.6.1       fansi_1.0.4     vctrs_0.6.3    
[13] pbdZMQ_0.3-9    compiler_4.3.0  tools_4.3.0     evaluate_0.21  
[17] pillar_1.9.0    crayon_1.5.2    rlang_1.1.1     jsonlite_1.8.5 
[21] IRkernel_1.3.2  uuid_1.1-0     

Base R#

base::library(help = 'base')
base::library(help = 'utils')
base::library(help = 'datasets')

utils::head(iris)
A data.frame: 6 × 5
Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpecies
<dbl><dbl><dbl><dbl><fct>
15.13.51.40.2setosa
24.93.01.40.2setosa
34.73.21.30.2setosa
44.63.11.50.2setosa
55.03.61.40.2setosa
65.43.91.70.4setosa

Select the element in the 10th row and the 5th column of the iris data set.

                                   datasets::iris[10, 5]
                base::as.character(datasets::iris[10, 5])
base::as.factor(base::as.character(datasets::iris[10, 5]))
setosa
Levels:
  1. 'setosa'
  2. 'versicolor'
  3. 'virginica'
'setosa'
setosa
Levels: 'setosa'

Select the species column of the iris data set and print the last five rows of that column.

             class(iris$Species)
                   iris$Species
class(as.character(iris$Species))
      as.character(iris$Species)

             class(iris[['Species']])
                   iris[['Species']]
class(as.character(iris[['Species']]))
      as.character(iris[['Species']])

class(iris['Species'])
      iris['Species']
'factor'
  1. setosa
  2. setosa
  3. setosa
  4. setosa
  5. setosa
  6. setosa
  7. setosa
  8. setosa
  9. setosa
  10. setosa
  11. setosa
  12. setosa
  13. setosa
  14. setosa
  15. setosa
  16. setosa
  17. setosa
  18. setosa
  19. setosa
  20. setosa
  21. setosa
  22. setosa
  23. setosa
  24. setosa
  25. setosa
  26. setosa
  27. setosa
  28. setosa
  29. setosa
  30. setosa
  31. setosa
  32. setosa
  33. setosa
  34. setosa
  35. setosa
  36. setosa
  37. setosa
  38. setosa
  39. setosa
  40. setosa
  41. setosa
  42. setosa
  43. setosa
  44. setosa
  45. setosa
  46. setosa
  47. setosa
  48. setosa
  49. setosa
  50. setosa
  51. versicolor
  52. versicolor
  53. versicolor
  54. versicolor
  55. versicolor
  56. versicolor
  57. versicolor
  58. versicolor
  59. versicolor
  60. versicolor
  61. versicolor
  62. versicolor
  63. versicolor
  64. versicolor
  65. versicolor
  66. versicolor
  67. versicolor
  68. versicolor
  69. versicolor
  70. versicolor
  71. versicolor
  72. versicolor
  73. versicolor
  74. versicolor
  75. versicolor
  76. versicolor
  77. versicolor
  78. versicolor
  79. versicolor
  80. versicolor
  81. versicolor
  82. versicolor
  83. versicolor
  84. versicolor
  85. versicolor
  86. versicolor
  87. versicolor
  88. versicolor
  89. versicolor
  90. versicolor
  91. versicolor
  92. versicolor
  93. versicolor
  94. versicolor
  95. versicolor
  96. versicolor
  97. versicolor
  98. versicolor
  99. versicolor
  100. versicolor
  101. virginica
  102. virginica
  103. virginica
  104. virginica
  105. virginica
  106. virginica
  107. virginica
  108. virginica
  109. virginica
  110. virginica
  111. virginica
  112. virginica
  113. virginica
  114. virginica
  115. virginica
  116. virginica
  117. virginica
  118. virginica
  119. virginica
  120. virginica
  121. virginica
  122. virginica
  123. virginica
  124. virginica
  125. virginica
  126. virginica
  127. virginica
  128. virginica
  129. virginica
  130. virginica
  131. virginica
  132. virginica
  133. virginica
  134. virginica
  135. virginica
  136. virginica
  137. virginica
  138. virginica
  139. virginica
  140. virginica
  141. virginica
  142. virginica
  143. virginica
  144. virginica
  145. virginica
  146. virginica
  147. virginica
  148. virginica
  149. virginica
  150. virginica
Levels:
  1. 'setosa'
  2. 'versicolor'
  3. 'virginica'
'character'
  1. 'setosa'
  2. 'setosa'
  3. 'setosa'
  4. 'setosa'
  5. 'setosa'
  6. 'setosa'
  7. 'setosa'
  8. 'setosa'
  9. 'setosa'
  10. 'setosa'
  11. 'setosa'
  12. 'setosa'
  13. 'setosa'
  14. 'setosa'
  15. 'setosa'
  16. 'setosa'
  17. 'setosa'
  18. 'setosa'
  19. 'setosa'
  20. 'setosa'
  21. 'setosa'
  22. 'setosa'
  23. 'setosa'
  24. 'setosa'
  25. 'setosa'
  26. 'setosa'
  27. 'setosa'
  28. 'setosa'
  29. 'setosa'
  30. 'setosa'
  31. 'setosa'
  32. 'setosa'
  33. 'setosa'
  34. 'setosa'
  35. 'setosa'
  36. 'setosa'
  37. 'setosa'
  38. 'setosa'
  39. 'setosa'
  40. 'setosa'
  41. 'setosa'
  42. 'setosa'
  43. 'setosa'
  44. 'setosa'
  45. 'setosa'
  46. 'setosa'
  47. 'setosa'
  48. 'setosa'
  49. 'setosa'
  50. 'setosa'
  51. 'versicolor'
  52. 'versicolor'
  53. 'versicolor'
  54. 'versicolor'
  55. 'versicolor'
  56. 'versicolor'
  57. 'versicolor'
  58. 'versicolor'
  59. 'versicolor'
  60. 'versicolor'
  61. 'versicolor'
  62. 'versicolor'
  63. 'versicolor'
  64. 'versicolor'
  65. 'versicolor'
  66. 'versicolor'
  67. 'versicolor'
  68. 'versicolor'
  69. 'versicolor'
  70. 'versicolor'
  71. 'versicolor'
  72. 'versicolor'
  73. 'versicolor'
  74. 'versicolor'
  75. 'versicolor'
  76. 'versicolor'
  77. 'versicolor'
  78. 'versicolor'
  79. 'versicolor'
  80. 'versicolor'
  81. 'versicolor'
  82. 'versicolor'
  83. 'versicolor'
  84. 'versicolor'
  85. 'versicolor'
  86. 'versicolor'
  87. 'versicolor'
  88. 'versicolor'
  89. 'versicolor'
  90. 'versicolor'
  91. 'versicolor'
  92. 'versicolor'
  93. 'versicolor'
  94. 'versicolor'
  95. 'versicolor'
  96. 'versicolor'
  97. 'versicolor'
  98. 'versicolor'
  99. 'versicolor'
  100. 'versicolor'
  101. 'virginica'
  102. 'virginica'
  103. 'virginica'
  104. 'virginica'
  105. 'virginica'
  106. 'virginica'
  107. 'virginica'
  108. 'virginica'
  109. 'virginica'
  110. 'virginica'
  111. 'virginica'
  112. 'virginica'
  113. 'virginica'
  114. 'virginica'
  115. 'virginica'
  116. 'virginica'
  117. 'virginica'
  118. 'virginica'
  119. 'virginica'
  120. 'virginica'
  121. 'virginica'
  122. 'virginica'
  123. 'virginica'
  124. 'virginica'
  125. 'virginica'
  126. 'virginica'
  127. 'virginica'
  128. 'virginica'
  129. 'virginica'
  130. 'virginica'
  131. 'virginica'
  132. 'virginica'
  133. 'virginica'
  134. 'virginica'
  135. 'virginica'
  136. 'virginica'
  137. 'virginica'
  138. 'virginica'
  139. 'virginica'
  140. 'virginica'
  141. 'virginica'
  142. 'virginica'
  143. 'virginica'
  144. 'virginica'
  145. 'virginica'
  146. 'virginica'
  147. 'virginica'
  148. 'virginica'
  149. 'virginica'
  150. 'virginica'
'factor'
  1. setosa
  2. setosa
  3. setosa
  4. setosa
  5. setosa
  6. setosa
  7. setosa
  8. setosa
  9. setosa
  10. setosa
  11. setosa
  12. setosa
  13. setosa
  14. setosa
  15. setosa
  16. setosa
  17. setosa
  18. setosa
  19. setosa
  20. setosa
  21. setosa
  22. setosa
  23. setosa
  24. setosa
  25. setosa
  26. setosa
  27. setosa
  28. setosa
  29. setosa
  30. setosa
  31. setosa
  32. setosa
  33. setosa
  34. setosa
  35. setosa
  36. setosa
  37. setosa
  38. setosa
  39. setosa
  40. setosa
  41. setosa
  42. setosa
  43. setosa
  44. setosa
  45. setosa
  46. setosa
  47. setosa
  48. setosa
  49. setosa
  50. setosa
  51. versicolor
  52. versicolor
  53. versicolor
  54. versicolor
  55. versicolor
  56. versicolor
  57. versicolor
  58. versicolor
  59. versicolor
  60. versicolor
  61. versicolor
  62. versicolor
  63. versicolor
  64. versicolor
  65. versicolor
  66. versicolor
  67. versicolor
  68. versicolor
  69. versicolor
  70. versicolor
  71. versicolor
  72. versicolor
  73. versicolor
  74. versicolor
  75. versicolor
  76. versicolor
  77. versicolor
  78. versicolor
  79. versicolor
  80. versicolor
  81. versicolor
  82. versicolor
  83. versicolor
  84. versicolor
  85. versicolor
  86. versicolor
  87. versicolor
  88. versicolor
  89. versicolor
  90. versicolor
  91. versicolor
  92. versicolor
  93. versicolor
  94. versicolor
  95. versicolor
  96. versicolor
  97. versicolor
  98. versicolor
  99. versicolor
  100. versicolor
  101. virginica
  102. virginica
  103. virginica
  104. virginica
  105. virginica
  106. virginica
  107. virginica
  108. virginica
  109. virginica
  110. virginica
  111. virginica
  112. virginica
  113. virginica
  114. virginica
  115. virginica
  116. virginica
  117. virginica
  118. virginica
  119. virginica
  120. virginica
  121. virginica
  122. virginica
  123. virginica
  124. virginica
  125. virginica
  126. virginica
  127. virginica
  128. virginica
  129. virginica
  130. virginica
  131. virginica
  132. virginica
  133. virginica
  134. virginica
  135. virginica
  136. virginica
  137. virginica
  138. virginica
  139. virginica
  140. virginica
  141. virginica
  142. virginica
  143. virginica
  144. virginica
  145. virginica
  146. virginica
  147. virginica
  148. virginica
  149. virginica
  150. virginica
Levels:
  1. 'setosa'
  2. 'versicolor'
  3. 'virginica'
'character'
  1. 'setosa'
  2. 'setosa'
  3. 'setosa'
  4. 'setosa'
  5. 'setosa'
  6. 'setosa'
  7. 'setosa'
  8. 'setosa'
  9. 'setosa'
  10. 'setosa'
  11. 'setosa'
  12. 'setosa'
  13. 'setosa'
  14. 'setosa'
  15. 'setosa'
  16. 'setosa'
  17. 'setosa'
  18. 'setosa'
  19. 'setosa'
  20. 'setosa'
  21. 'setosa'
  22. 'setosa'
  23. 'setosa'
  24. 'setosa'
  25. 'setosa'
  26. 'setosa'
  27. 'setosa'
  28. 'setosa'
  29. 'setosa'
  30. 'setosa'
  31. 'setosa'
  32. 'setosa'
  33. 'setosa'
  34. 'setosa'
  35. 'setosa'
  36. 'setosa'
  37. 'setosa'
  38. 'setosa'
  39. 'setosa'
  40. 'setosa'
  41. 'setosa'
  42. 'setosa'
  43. 'setosa'
  44. 'setosa'
  45. 'setosa'
  46. 'setosa'
  47. 'setosa'
  48. 'setosa'
  49. 'setosa'
  50. 'setosa'
  51. 'versicolor'
  52. 'versicolor'
  53. 'versicolor'
  54. 'versicolor'
  55. 'versicolor'
  56. 'versicolor'
  57. 'versicolor'
  58. 'versicolor'
  59. 'versicolor'
  60. 'versicolor'
  61. 'versicolor'
  62. 'versicolor'
  63. 'versicolor'
  64. 'versicolor'
  65. 'versicolor'
  66. 'versicolor'
  67. 'versicolor'
  68. 'versicolor'
  69. 'versicolor'
  70. 'versicolor'
  71. 'versicolor'
  72. 'versicolor'
  73. 'versicolor'
  74. 'versicolor'
  75. 'versicolor'
  76. 'versicolor'
  77. 'versicolor'
  78. 'versicolor'
  79. 'versicolor'
  80. 'versicolor'
  81. 'versicolor'
  82. 'versicolor'
  83. 'versicolor'
  84. 'versicolor'
  85. 'versicolor'
  86. 'versicolor'
  87. 'versicolor'
  88. 'versicolor'
  89. 'versicolor'
  90. 'versicolor'
  91. 'versicolor'
  92. 'versicolor'
  93. 'versicolor'
  94. 'versicolor'
  95. 'versicolor'
  96. 'versicolor'
  97. 'versicolor'
  98. 'versicolor'
  99. 'versicolor'
  100. 'versicolor'
  101. 'virginica'
  102. 'virginica'
  103. 'virginica'
  104. 'virginica'
  105. 'virginica'
  106. 'virginica'
  107. 'virginica'
  108. 'virginica'
  109. 'virginica'
  110. 'virginica'
  111. 'virginica'
  112. 'virginica'
  113. 'virginica'
  114. 'virginica'
  115. 'virginica'
  116. 'virginica'
  117. 'virginica'
  118. 'virginica'
  119. 'virginica'
  120. 'virginica'
  121. 'virginica'
  122. 'virginica'
  123. 'virginica'
  124. 'virginica'
  125. 'virginica'
  126. 'virginica'
  127. 'virginica'
  128. 'virginica'
  129. 'virginica'
  130. 'virginica'
  131. 'virginica'
  132. 'virginica'
  133. 'virginica'
  134. 'virginica'
  135. 'virginica'
  136. 'virginica'
  137. 'virginica'
  138. 'virginica'
  139. 'virginica'
  140. 'virginica'
  141. 'virginica'
  142. 'virginica'
  143. 'virginica'
  144. 'virginica'
  145. 'virginica'
  146. 'virginica'
  147. 'virginica'
  148. 'virginica'
  149. 'virginica'
  150. 'virginica'
'data.frame'
A data.frame: 150 × 1
Species
<fct>
setosa
setosa
setosa
setosa
setosa
setosa
setosa
setosa
setosa
setosa
setosa
setosa
setosa
setosa
setosa
setosa
setosa
setosa
setosa
setosa
setosa
setosa
setosa
setosa
setosa
setosa
setosa
setosa
setosa
setosa
virginica
virginica
virginica
virginica
virginica
virginica
virginica
virginica
virginica
virginica
virginica
virginica
virginica
virginica
virginica
virginica
virginica
virginica
virginica
virginica
virginica
virginica
virginica
virginica
virginica
virginica
virginica
virginica
virginica
virginica
             iris[(length(iris$Species) - 4):length(iris$Species),'Species']
as.character(iris[(length(iris$Species) - 4):length(iris$Species),'Species'])

             tail(iris$Species, n = 5)
as.character(tail(iris$Species, n = 5))

             tail(iris[['Species']], n = 5)
as.character(tail(iris[['Species']], n = 5))

tail(iris['Species'], n = 5)
  1. virginica
  2. virginica
  3. virginica
  4. virginica
  5. virginica
Levels:
  1. 'setosa'
  2. 'versicolor'
  3. 'virginica'
  1. 'virginica'
  2. 'virginica'
  3. 'virginica'
  4. 'virginica'
  5. 'virginica'
  1. virginica
  2. virginica
  3. virginica
  4. virginica
  5. virginica
Levels:
  1. 'setosa'
  2. 'versicolor'
  3. 'virginica'
  1. 'virginica'
  2. 'virginica'
  3. 'virginica'
  4. 'virginica'
  5. 'virginica'
  1. virginica
  2. virginica
  3. virginica
  4. virginica
  5. virginica
Levels:
  1. 'setosa'
  2. 'versicolor'
  3. 'virginica'
  1. 'virginica'
  2. 'virginica'
  3. 'virginica'
  4. 'virginica'
  5. 'virginica'
A data.frame: 5 × 1
Species
<fct>
146virginica
147virginica
148virginica
149virginica
150virginica

What is the average sepal length of flowers with petal widths greater than one?

base::mean(iris$Sepal.Length[iris$Petal.Width > 1])
6.3258064516129

Write a for loop that iterates over the numbers 1 to 10. If the number is less than five, print the product of the number with 3; otherwise, print the product of the number with 2.

for (i in 1:10) {
  if (i < 5) {
    base::print(i * 3)
  } else {
    base::print(i * 2)
  }
}
[1] 3
[1] 6
[1] 9
[1] 12
[1] 10
[1] 12
[1] 14
[1] 16
[1] 18
[1] 20

Write a while loop that prints out random normal numbers with mean equal to 1 and standard deviation equal to 2 (use rnorm()) but breaks for a number greater than 3.

base::set.seed(184)

num <- stats::rnorm(n = 1, mean = 1, sd = 2)
while (num < 3) {
  base::print(num)
  num <- stats::rnorm(n = 1, mean = 1, sd = 2)
}
[1] 0.1167419
[1] 1.588951
[1] 2.436941
[1] 0.932507
[1] 2.408507
[1] -1.206123
[1] -1.158457
base::set.seed(184)

while (TRUE) {
  num <- stats::rnorm(n = 1, mean = 1, sd = 2)

  if (num > 3) {
    break
  }

  base::print(num)
}
[1] 0.1167419
[1] 1.588951
[1] 2.436941
[1] 0.932507
[1] 2.408507
[1] -1.206123
[1] -1.158457

Use a nested for loop to produce the following data frame. Name the data frame Counting and the variables V1, V2, V3, and V4.

V1

V2

V3

V4

1

2

3

4

2

3

4

5

3

4

5

6

Counting <- base::data.frame()
Rows     <- 1:3
Cols     <- 1:4

for (j in 1:base::length(Cols)) {
  for (i in 1:base::length(Rows)) {
    Counting[i, j] <- i + j - 1
  }
}

Counting
A data.frame: 3 × 4
V1V2V3V4
<dbl><dbl><dbl><dbl>
11234
22345
33456
base::colnames(Counting) <- base::sub('V', 'X', base::colnames(Counting))

Counting
A data.frame: 3 × 4
X1X2X3X4
<dbl><dbl><dbl><dbl>
11234
22345
33456

Complete the following user-defined function to generate n random normal numbers with a given mean and standard deviation and plot a histogram of it (use hist()). Call the function to generate 1000 random normal numbers with mean equal to 0 and standard deviation equal to 1 and plot its histogram.

base::set.seed(184)

plotNormal <- function (repetitions, mean, sd) {
  graphics::hist(
    col  = 'blue',
    main = base::paste('Histogram from a Normal(', mean, ',' , sd, ') Distribution'),
    x    = stats::rnorm(n = repetitions, mean = mean, sd = sd)
  )
}
plotNormal(1000, 0, 1)
../../../../_images/a09187037cca1a793c3a6b3425b34d5361746faba096295d4548db999eaa7d30.png

Vectors c(), :, seq(), rep()

Subsetting [] (vectors, data frames), [[]] (lists), $ (data frames, lists)

base::class(x = v)
utils::str(object = v)
base::summary(object = v)
# vector of doubles
v <- base::c(2, 5, NA, 5)
v

base::is.vector(v)
base::is.double(v)
base::is.atomic(v)
base::is.integer(v)
base::typeof(v)

base::mean(x = g)
base::mean(x = g, na.rm = TRUE)
  1. 2
  2. 5
  3. <NA>
  4. 5
TRUE
TRUE
TRUE
FALSE
'double'
Error in eval(expr, envir, enclos): object 'g' not found
Traceback:

1. base::mean(x = g)
# vector of integers
v <- base::c(2L, 5L, NA, 5L)
v

base::is.vector(v)
base::is.double(v)
base::is.atomic(v)
base::is.integer(v)
base::typeof(v)

base::mean(x = g)
base::mean(x = g, na.rm = TRUE)
  1. 2
  2. 5
  3. <NA>
  4. 5
TRUE
FALSE
TRUE
TRUE
'integer'
<NA>
4
# vector of integers
v <- 2:6
v

base::is.vector(v)
base::is.double(v)
base::is.atomic(v)
base::is.integer(v)
base::typeof(v)
  1. 2
  2. 3
  3. 4
  4. 5
  5. 6
TRUE
FALSE
TRUE
TRUE
'integer'
# vector of doubles
v <- seq(2, 3, by = 0.2)
v

base::is.vector(v)
base::is.double(v)
base::is.atomic(v)
base::is.integer(v)
base::typeof(v)
  1. 2
  2. 2.2
  3. 2.4
  4. 2.6
  5. 2.8
  6. 3
TRUE
TRUE
TRUE
FALSE
'double'
# vector of doubles
v <- seq(2, 5, length.out = 5)
v

base::is.vector(v)
base::is.double(v)
base::is.atomic(v)
base::is.integer(v)
base::typeof(v)
  1. 2
  2. 2.75
  3. 3.5
  4. 4.25
  5. 5
TRUE
TRUE
TRUE
FALSE
'double'
# vector of characters
v <- rep(c('a', 'b', 'c'), times = 3)
v

base::is.vector(v)
base::is.double(v)
base::is.atomic(v)
base::is.integer(v)
base::is.character(v)
base::typeof(v)
  1. 'a'
  2. 'b'
  3. 'c'
  4. 'a'
  5. 'b'
  6. 'c'
  7. 'a'
  8. 'b'
  9. 'c'
TRUE
FALSE
TRUE
FALSE
TRUE
'character'
# vector of characters
v <- rep(c('a', 'b', 'c'), each = 3)
v

base::is.vector(v)
base::is.double(v)
base::is.atomic(v)
base::is.integer(v)
base::is.character(v)
base::typeof(v)
  1. 'a'
  2. 'a'
  3. 'a'
  4. 'b'
  5. 'b'
  6. 'b'
  7. 'c'
  8. 'c'
  9. 'c'
TRUE
FALSE
TRUE
FALSE
TRUE
'character'
v <- c('a', 'b', 'c', 1, 2, 3)
v

base::is.vector(v)
base::is.double(v)
base::is.atomic(v)
base::is.integer(v)
base::is.character(v)
base::typeof(v)
  1. 'a'
  2. 'b'
  3. 'c'
  4. '1'
  5. '2'
  6. '3'
TRUE
FALSE
TRUE
FALSE
TRUE
'character'
v[4]

v[2:4]

v[c(1, 5)]

v[-4]

v[-c(1, 4, 6)]
'1'
  1. 'b'
  2. 'c'
  3. '1'
  1. 'a'
  2. '2'
  1. 'a'
  2. 'b'
  3. 'c'
  4. '2'
  5. '3'
  1. 'b'
  2. 'c'
  3. '2'
v1 <- c('a', 'b', 'c', 'd', 'e', 'f')
v2 <- 1:6

df <- base::data.frame(v1, v2)
df
A data.frame: 6 × 2
v1v2
<chr><int>
a1
b2
c3
d4
e5
f6
df[3, 2]
3
df[4,]
A data.frame: 1 × 2
v1v2
<chr><int>
4d4
df[,2]
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
base::set.seed(184)

y <- base::sample(v1, 10, replace = TRUE)
y

y == 'g'

y[y == 'g']

y[y %in% c('e', 'f', 'g')]

base::which(y %in% c('e', 'f', 'g'))
  1. 'c'
  2. 'f'
  3. 'f'
  4. 'f'
  5. 'f'
  6. 'a'
  7. 'f'
  8. 'e'
  9. 'f'
  10. 'c'
  1. FALSE
  2. FALSE
  3. FALSE
  4. FALSE
  5. FALSE
  6. FALSE
  7. FALSE
  8. FALSE
  9. FALSE
  10. FALSE
  1. 'f'
  2. 'f'
  3. 'f'
  4. 'f'
  5. 'f'
  6. 'e'
  7. 'f'
  1. 2
  2. 3
  3. 4
  4. 5
  5. 7
  6. 8
  7. 9
numbers <- 1:26
letters <- base::letters

df <- base::data.frame(numbers, letters)
df
A data.frame: 26 × 2
numbersletters
<int><chr>
1a
2b
3c
4d
5e
6f
7g
8h
9i
10j
11k
12l
13m
14n
15o
16p
17q
18r
19s
20t
21u
22v
23w
24x
25y
26z
df$numbers
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
df$combo     <- base::paste(letters, '-', numbers, 'combo')
df$uppercase <- base::LETTERS
df$states    <- NA

for (i in 1:base::nrow(df)) {
  df$states[i] <- datasets::state.abb[i]
}

df
A data.frame: 26 × 5
numbersletterscombouppercasestates
<int><chr><chr><chr><chr>
1aa - 1 combo AAL
2bb - 2 combo BAK
3cc - 3 combo CAZ
4dd - 4 combo DAR
5ee - 5 combo ECA
6ff - 6 combo FCO
7gg - 7 combo GCT
8hh - 8 combo HDE
9ii - 9 combo IFL
10jj - 10 comboJGA
11kk - 11 comboKHI
12ll - 12 comboLID
13mm - 13 comboMIL
14nn - 14 comboNIN
15oo - 15 comboOIA
16pp - 16 comboPKS
17qq - 17 comboQKY
18rr - 18 comboRLA
19ss - 19 comboSME
20tt - 20 comboTMD
21uu - 21 comboUMA
22vv - 22 comboVMI
23ww - 23 comboWMN
24xx - 24 comboXMS
25yy - 25 comboYMO
26zz - 26 comboZMT
for (i in 1:4) {
  j <- i + 10
  base::print(j)
}
[1] 11
[1] 12
[1] 13
[1] 14
byFives <- base::seq(0, 100, by = 5)
byFives

for (i in 1:base::length(byFives)) {
  j <- byFives[i] / 3
  base::print(j)
}
  1. 0
  2. 5
  3. 10
  4. 15
  5. 20
  6. 25
  7. 30
  8. 35
  9. 40
  10. 45
  11. 50
  12. 55
  13. 60
  14. 65
  15. 70
  16. 75
  17. 80
  18. 85
  19. 90
  20. 95
  21. 100
[1] 0
[1] 1.666667
[1] 3.333333
[1] 5
[1] 6.666667
[1] 8.333333
[1] 10
[1] 11.66667
[1] 13.33333
[1] 15
[1] 16.66667
[1] 18.33333
[1] 20
[1] 21.66667
[1] 23.33333
[1] 25
[1] 26.66667
[1] 28.33333
[1] 30
[1] 31.66667
[1] 33.33333
numbers <- base::seq(10, 37, by = 1)
numbers

DivisibleBy5 <- rep(NA, length(numbers))
DivisibleBy5

for (i in 1:base::length(numbers)) {
  remainder <- numbers[i] %% 5
  if (remainder == 0) {
    DivisibleBy5[i] <- TRUE
  } else {
    DivisibleBy5[i] <- FALSE
  }
}

# ALTERNATIVE APPROACH
# index5 = 1
# for (i in numbers) {
#   remainder <- i %% 5
#   if (remainder == 0) {
#     DivisibleBy5[index5] <- TRUE
#   } else {
#     DivisibleBy5[index5] <- FALSE
#   }
#   index5 <- index5 + 1
# }

        DivisibleBy5
numbers[DivisibleBy5]
  1. 10
  2. 11
  3. 12
  4. 13
  5. 14
  6. 15
  7. 16
  8. 17
  9. 18
  10. 19
  11. 20
  12. 21
  13. 22
  14. 23
  15. 24
  16. 25
  17. 26
  18. 27
  19. 28
  20. 29
  21. 30
  22. 31
  23. 32
  24. 33
  25. 34
  26. 35
  27. 36
  28. 37
  1. <NA>
  2. <NA>
  3. <NA>
  4. <NA>
  5. <NA>
  6. <NA>
  7. <NA>
  8. <NA>
  9. <NA>
  10. <NA>
  11. <NA>
  12. <NA>
  13. <NA>
  14. <NA>
  15. <NA>
  16. <NA>
  17. <NA>
  18. <NA>
  19. <NA>
  20. <NA>
  21. <NA>
  22. <NA>
  23. <NA>
  24. <NA>
  25. <NA>
  26. <NA>
  27. <NA>
  28. <NA>
  1. TRUE
  2. FALSE
  3. FALSE
  4. FALSE
  5. FALSE
  6. TRUE
  7. FALSE
  8. FALSE
  9. FALSE
  10. FALSE
  11. TRUE
  12. FALSE
  13. FALSE
  14. FALSE
  15. FALSE
  16. TRUE
  17. FALSE
  18. FALSE
  19. FALSE
  20. FALSE
  21. TRUE
  22. FALSE
  23. FALSE
  24. FALSE
  25. FALSE
  26. TRUE
  27. FALSE
  28. FALSE
  1. 10
  2. 15
  3. 20
  4. 25
  5. 30
  6. 35
        numbers %% 5 == 0
numbers[numbers %% 5 == 0]
  1. TRUE
  2. FALSE
  3. FALSE
  4. FALSE
  5. FALSE
  6. TRUE
  7. FALSE
  8. FALSE
  9. FALSE
  10. FALSE
  11. TRUE
  12. FALSE
  13. FALSE
  14. FALSE
  15. FALSE
  16. TRUE
  17. FALSE
  18. FALSE
  19. FALSE
  20. FALSE
  21. TRUE
  22. FALSE
  23. FALSE
  24. FALSE
  25. FALSE
  26. TRUE
  27. FALSE
  28. FALSE
  1. 10
  2. 15
  3. 20
  4. 25
  5. 30
  6. 35
for.loop      <- DivisibleBy5
vectorization <- numbers %% 5 == 0

base::sum(for.loop != vectorization) # = 0
0
df   <- data.frame()
Rows <- 1:6
Cols <- 1:5

for (j in 1:base::length(Cols)) {
  for (i in 1:base::length(Rows)) {
    df[i, j] <- paste('Row', i, ' Col', j)
  }
}

df
A data.frame: 6 × 5
V1V2V3V4V5
<chr><chr><chr><chr><chr>
1Row 1 Col 1Row 1 Col 2Row 1 Col 3Row 1 Col 4Row 1 Col 5
2Row 2 Col 1Row 2 Col 2Row 2 Col 3Row 2 Col 4Row 2 Col 5
3Row 3 Col 1Row 3 Col 2Row 3 Col 3Row 3 Col 4Row 3 Col 5
4Row 4 Col 1Row 4 Col 2Row 4 Col 3Row 4 Col 4Row 4 Col 5
5Row 5 Col 1Row 5 Col 2Row 5 Col 3Row 5 Col 4Row 5 Col 5
6Row 6 Col 1Row 6 Col 2Row 6 Col 3Row 6 Col 4Row 6 Col 5