[Solved] How to select certain character? [closed]


Please add a reproducible sample next time. Below is the sample input based on the data from the given link.

Input:

url <- c("/history/apollo/",    "/shuttle/countdown/",  "/shuttle/missions/sts-73/mission-sts-73.html", 
         "/shuttle/countdown/liftoff.html", "/shuttle/missions/sts-73/sts-73-patch-small.gif",  
         "/images/NASA-logosmall.gif",  "/shuttle/countdown/video/livevideo.gif",   
         "/shuttle/countdown/countdown.html",   "/shuttle/countdown/",  "https://stackoverflow.com/",    "/shuttle/countdown/count.gif", 
         "/images/NASA-logosmall.gif",  "/images/KSC-logosmall.gif",    "/shuttle/countdown/count.gif", 
         "/images/NASA-logosmall.gif",  "/images/KSC-logosmall.gif",    "/images/ksclogo-medium.gif",   
         "/images/launch-logo.gif", "/facts/about_ksc.html",    "/shuttle/missions/sts-71/images/KSC-95EC-0916.jpg")

return_code <- c(200, 200, 200, 304, 200, 304, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 304, 200, 200, 200)

df <- tibble(url, return_code)
df

# A tibble: 20 x 2
   url                                               return_code
   <chr>                                                   <dbl>
 1 /history/apollo/                                         200.
 2 /shuttle/countdown/                                      200.
 3 /shuttle/missions/sts-73/mission-sts-73.html             200.
 4 /shuttle/countdown/liftoff.html                          304.
 5 /shuttle/missions/sts-73/sts-73-p=atch-small.gif         200.
 6 /images/NASA-logosmall.gif                               304.
 7 /shuttle/countdown/video/livevideo.gif                   200.
 8 /shuttle/countdown/countdown.html                        200.
 9 /shuttle/countdown/                                      200.
10 /                                                        200.
11 /shuttle/countdown/count.gif                             200.
12 /images/NASA-logosmall.gif                               200.
13 /images/KSC-logosmall.gif                                200.
14 /shuttle/countdown/count.gif                             200.
15 /images/NASA-logosmall.gif                               200.
16 /images/KSC-logosmall.gif                                200.
17 /images/ksclogo-medium.gif                               304.
18 /images/launch-logo.gif                                  200.
19 /facts/about_ksc.html                                    200.
20 /shuttle/missions/sts-71/images/KSC-95EC-0916.jpg        200.

Method:

library(tidyverse)

# Custom function to select the last vector from the str_split() output 
find_gif <- function(myDF) {
  map_chr(myDF, ~(.x %>% last()))
}

df2 <- df %>% 
  filter(grepl(".gif$", url), return_code == 200) %>% 
  mutate(url2 = find_gif(str_split(url, "[/]"))) %>% 
  select(url2, return_code)

Output:

df2
# A tibble: 9 x 2
  url2                    return_code
  <chr>                         <dbl>
1 sts-73-patch-small.gif         200.
2 livevideo.gif                  200.
3 count.gif                      200.
4 NASA-logosmall.gif             200.
5 KSC-logosmall.gif              200.
6 count.gif                      200.
7 NASA-logosmall.gif             200.
8 KSC-logosmall.gif              200.
9 launch-logo.gif                200.

If you want only the unique values, use the unique() function.

df2 %>% select(url2) %>% unique()
# A tibble: 6 x 1
  url2                  
  <chr>                 
1 sts-73-patch-small.gif
2 livevideo.gif         
3 count.gif             
4 NASA-logosmall.gif    
5 KSC-logosmall.gif     
6 launch-logo.gif 

solved How to select certain character? [closed]