Programming in R. Ex3. Detailed Expl
Programming in R. Ex3. Detailed Expl
Detailed explanation
● Explanation:
○ things: A variable storing a vector of strings.
○ c(): Combines individual string elements into a vector. This is a way to
create a list-like collection in R.
○ The vector contains five string elements representing a mix of words with the
substring "ab" in different contexts.
● grep("ab", things):
○ Searches for the pattern "ab" in each element of things.
○ Returns the indices of elements containing "ab".
● things[...]:
○ Accesses elements of things at the positions returned by grep.
○ Output: Elements containing "ab": c("abcd", "cdab", "cabdab", "c
ab", "ab").
● value = TRUE:
○ Instead of returning indices, it directly returns the matching elements.
Boolean Check for Matches:
r
Копировать код
grepl("ab", things)
● grepl():
○ Similar to grep, but instead of indices, it returns a logical vector.
○ Each element corresponds to whether the string contains "ab" (TRUE or
FALSE).
● str_replace():
○ Replaces the first occurrence of "ab" with "AB" in each string.
○ Example: "abcd" → "ABcd".
● str_replace_all():
○ Replaces every occurrence of "ab" in each string.
● str_c():
○ Joins the path and filename with a specified separator ("/").
○ Example: "www.path.cbs.dk" and "filename.txt" →
"www.path.cbs.dk/filename.txt".
Exercise 2: Writing a Simple Loop
This exercise introduces the concept of loops in R to iterate through a vector of strings and
perform operations on each element.
for (i in strings) {
print(i)
}
Output:
csharp
Копировать код
[1] "abcd"
[1] "cdab"
[1] "cabdab"
[1] "c ab"
[1] "ab"
● 1:length(strings):
○ length(strings) returns the number of elements in the strings vector,
which is 5.
○ 1:length(strings) generates a sequence of numbers from 1 to 5 ([1,
2, 3, 4, 5]).
○ This sequence represents the indices of the vector strings.
● for (i in 1:length(strings)):
○ Loops through each index of the strings vector.
○ The variable i takes on values 1, 2, 3, 4, 5 during the iterations.
● strings[i]:
○ Accesses the i-th element of strings using the index i.
● print(strings[i]):
○ Prints the current element of strings based on the index i.
Output:
csharp
Копировать код
[1] "abcd"
[1] "cdab"
[1] "cabdab"
[1] "c ab"
[1] "ab"
Output:
csharp
Копировать код
[1] TRUE
[1] TRUE
[1] FALSE
[1] FALSE
[1] FALSE
● sapply():
○ Stands for "simplified apply."
○ Applies the specified function (identity() in this case) to each element of
the strings vector.
● identity():
○ A function that returns its input without modification. Essentially, it does
nothing to the input.
● This is a vectorized alternative to the loop, and it outputs all elements of the vector as
they are.
Output:
arduino
Копировать код
abcd cdab cabdab c ab ab
"abcd" "cdab" "cabdab" "c ab" "ab"
Summary of Exercise 2
1. For-Loops: Useful for iterating through vectors and performing operations element-
by-element. R allows iteration over:
○ Direct elements (e.g., for (i in strings)).
○ Indices (e.g., for (i in 1:length(strings))).
2. Substring Checks:
○ Use grepl() to determine whether a specific substring exists within a string.
3. Vectorized Alternatives:
○ Use sapply() to apply functions to each element of a vector efficiently.
○ Results are often faster and more concise than loops.