Ex3.
Detailed explanation
Exercise 1: String Handling
Code:
r
Копировать код
things <- c("abcd", "cdab", "cabdab", "c ab", "ab")
● 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.
Find All Positions of "ab":
r
Копировать код
things[grep("ab", things)]
● 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").
Output Matching Elements Directly:
r
Копировать код
grep("ab", things, value = TRUE)
● 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).
Replace "ab" with "AB":
r
Копировать код
str_replace(things, "ab", "AB")
● str_replace():
○ Replaces the first occurrence of "ab" with "AB" in each string.
○ Example: "abcd" → "ABcd".
Replace All Occurrences:
r
Копировать код
str_replace_all(things, "ab", "AB")
● str_replace_all():
○ Replaces every occurrence of "ab" in each string.
Concatenate Path and File Name:
r
Копировать код
str_c(path, filename, sep = "/")
● 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.
Code 1: Printing Each Element Using a For-Loop
r
Копировать код
strings <- c("abcd", "cdab", "cabdab", "c ab", "ab")
for (i in strings) {
print(i)
}
● strings: A vector containing five string elements: ["abcd", "cdab",
"cabdab", "c ab", "ab"].
○ The vector will serve as the input for the loop.
● for (i in strings):
○ The for loop iterates over each element in the strings vector.
○ During each iteration, the current element is assigned to the variable i.
● print(i):
○ The print() function outputs the value of i, which represents the current
string in the iteration.
How the Loop Works Step by Step:
1. On the first iteration, i = "abcd", so print(i) outputs "abcd".
2. On the second iteration, i = "cdab", so print(i) outputs "cdab".
3. This continues for all elements in the strings vector.
Output:
csharp
Копировать код
[1] "abcd"
[1] "cdab"
[1] "cabdab"
[1] "c ab"
[1] "ab"
Code 2: Printing Elements by Index
r
Копировать код
for (i in 1:length(strings)) {
print(strings[i])
}
● 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.
How the Loop Works Step by Step:
1. On the first iteration, i = 1, so strings[1] = "abcd", and
print(strings[i]) outputs "abcd".
2. On the second iteration, i = 2, so strings[2] = "cdab", and
print(strings[i]) outputs "cdab".
3. This continues until i = 5, printing all elements.
Output:
csharp
Копировать код
[1] "abcd"
[1] "cdab"
[1] "cabdab"
[1] "c ab"
[1] "ab"
Code 3: Checking for a Substring and Printing Results
r
Копировать код
for (i in strings) {
print(grepl("cd", i))
}
● for (i in strings):
○ Similar to the first loop, it iterates over each string in the strings vector.
● grepl("cd", i):
○ The grepl() function checks whether the substring "cd" exists in the
current string (i).
○ Returns TRUE if "cd" is found, otherwise FALSE.
● print(grepl("cd", i)):
○ Prints the result of the grepl() function for each string.
How the Loop Works Step by Step:
1. On the first iteration, i = "abcd". Since "abcd" contains "cd", grepl("cd",
"abcd") returns TRUE.
2. On the second iteration, i = "cdab". Since "cdab" contains "cd", grepl("cd",
"cdab") returns TRUE.
3. On the third iteration, i = "cabdab". Since "cabdab" does not contain "cd",
grepl("cd", "cabdab") returns FALSE.
4. The loop continues for all strings.
Output:
csharp
Копировать код
[1] TRUE
[1] TRUE
[1] FALSE
[1] FALSE
[1] FALSE
Code 4: Using sapply() for Efficiency
r
Копировать код
sapply(strings, identity)
● 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.