Answers to the exercises are available here.
If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.
use a stringr function to merge this 3 strings .
x <- "I AM SAM. I AM SAM. SAM I AM"
y <- "THAT SAM-I-AM! THAT SAM-I-AM! I DO NOT LIKE THAT SAM-I-AM!"
z <- ""DO WOULD YOU LIKE GREEN EGGS AND HAM?"
Now use a vector which contains x,y,z and NA and make it a single sentence using paste ,do the same by the same function you used for exercise1 .Can you spot the difference .
Install the babynames dataset ,find the vector of length of the babynames using stringr functions. You may wonder nchar can do the same so why not use that ,try finding out the difference and let me know in the comments.
We often use substr to get part of the string ,in stringr world there exist a much powerful function which does almost the same thing . Create a string name with your name .
Use str_sub to get the last character and the last 5 characters .
In mtcars dataset rownames, find all cars of the brand Merc .
Use the same mtcars rownames ,find the total number of times “e” appears in that .
Suppose you have a string like this
j <- "The_quick_brown_fox_jumps_over_the_lazy_dog"
split it in words using a stringr function
On the same string I need the first word splitted but the rest intact ,help me to achieve that
Now for on the same string J
a> I want the first “_” replaced by “–”
b> I want all the “_” replaced by “–”
Many of the times ,you don’t want
NA to appear when you do some string manipulation but its sometimes necessary to replace NA as a character(rather than remove it) ,stringr provides a useful tool for that.
Now if I have a vector like this ,
na_string_vec <- c("The_quick_brown_fox_jumps_over_the_lazy_dog",NA)
How can you turn the NA into a character string .