1

I am trying to create a dataframe with function names from a package of interest. I have managed to do this in the following way:

# loading needed libraries
library(ggstatsplot)
library(tidyverse)

# creating a dataframe with namespace from package of interest
(
  df_ns <- getNamespaceExports(ns = "ggstatsplot") %>%
    tibble::as_data_frame(.) %>%
    dplyr::filter(.data = ., grepl("^gg|^grouped", value)) %>%
    dplyr::filter(.data = ., value != "ggcoefstats") %>%
    dplyr::mutate(
      .data = .,
      functions = paste("ggstatsplot::", value, sep = "")
    ) %>%
    dplyr::mutate(
      .data = .,
      version = dplyr::case_when(grepl("^grouped", value) ~ "grouped",
                                 TRUE ~ "basic")
    )
)
#> # A tibble: 10 x 3
#>    value                  functions                           version
#>    <chr>                  <chr>                               <chr>  
#>  1 grouped_gghistostats   ggstatsplot::grouped_gghistostats   grouped
#>  2 grouped_ggscatterstats ggstatsplot::grouped_ggscatterstats grouped
#>  3 grouped_ggbetweenstats ggstatsplot::grouped_ggbetweenstats grouped
#>  4 grouped_ggcorrmat      ggstatsplot::grouped_ggcorrmat      grouped
#>  5 grouped_ggpiestats     ggstatsplot::grouped_ggpiestats     grouped
#>  6 ggbetweenstats         ggstatsplot::ggbetweenstats         basic  
#>  7 ggpiestats             ggstatsplot::ggpiestats             basic  
#>  8 ggcorrmat              ggstatsplot::ggcorrmat              basic  
#>  9 gghistostats           ggstatsplot::gghistostats           basic  
#> 10 ggscatterstats         ggstatsplot::ggscatterstats         basic

So far so good. Now what I want is the following:
I want to remove the grouped_ prefix from all names (first five rows, i.e.) in the column value. But when I try to do this using str_split, all names get assigned "gghistostats" instead of the 5 different names. How can I do this?

# stripping `grouped_` prefix from `value` column
df_ns %>%
  dplyr::mutate(
    .data = .,
    value = dplyr::case_when(
      grepl("^grouped", value) ~ stringr::str_split(value, "_")[[1]][[2]],
      TRUE ~ value
    )
  )
#> # A tibble: 10 x 3
#>    value          functions                           version
#>    <chr>          <chr>                               <chr>  
#>  1 gghistostats   ggstatsplot::grouped_gghistostats   grouped
#>  2 gghistostats   ggstatsplot::grouped_ggscatterstats grouped
#>  3 gghistostats   ggstatsplot::grouped_ggbetweenstats grouped
#>  4 gghistostats   ggstatsplot::grouped_ggcorrmat      grouped
#>  5 gghistostats   ggstatsplot::grouped_ggpiestats     grouped
#>  6 ggbetweenstats ggstatsplot::ggbetweenstats         basic  
#>  7 ggpiestats     ggstatsplot::ggpiestats             basic  
#>  8 ggcorrmat      ggstatsplot::ggcorrmat              basic  
#>  9 gghistostats   ggstatsplot::gghistostats           basic  
#> 10 ggscatterstats ggstatsplot::ggscatterstats         basic

Created on 2018-10-11 by the reprex package (v0.2.1)

Indrajeet Patil
  • 4,673
  • 2
  • 20
  • 51

2 Answers2

1

Try using sub:

df_ns$value <- sub("^grouped_", "", df_ns$value)

Or using a capture group:

df_ns$value <- sub("^grouped_(.*)", "\\1", df_ns$value, perl=TRUE)
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

We can use str_remove

library(stringr)
library(dplyr)
df_ns %>%
     mutate(value = str_remove(value, "^grouped_"))
akrun
  • 874,273
  • 37
  • 540
  • 662