Solution: Calculate String Length
See one of several solutions to the "Calculate String Length" challenge and compare your solution to it.
We'll cover the following...
Compare solutions
Please compare your code to the one given below. R is a flexible programming language. We can find the solution using extra functions or by following the steps in a different order. Please keep in mind that this solution is only one of many. Feel free to play with the code and try various methods. Here is our solution to the challenge in the previous lesson:
Press + to interact
config.py
hello.txt
library(stringr) # Call the librarieslibrary(readr) # Call the librariescreate_matrix <- function(x){# Code between the lines# ------text <- read_file('hello.txt') # Read the filemy_text <- str_to_lower(text) # Lowercase the textmy_text <- str_replace_all(my_text,'\n','') # Replace all newline characters with nothing-delete-my_text <- str_split(my_text,' ') # Split the text where blanks aremy_text <- my_text[[1]] # Select the first underlying elementwords <- my_text[str_detect(my_text,'sh')] # Detect all words involving 'sh'result <- str_length(words) # Calculate the lengths of words# ------#Please name the desired output as 'result'cat(result)}create_matrix(text)