assign_stars <- function(df, var, threshold, marker) {
require(dplyr)
require(rlang)
var <- sym(var)
val <- sym(paste(var, "pvalue" , sep="_"))
out <- sym(paste(var, "marker" , sep="_"))
mutate(df, !!out := if_else(!!val < threshold,
paste0(!!var, marker),
as.character(!!var)
)
)
}
If we wanted to do this only for one column, then following works:
df %>%
assign_stars(., "stub", 0.01, "***")
# # A tibble: 4 x 5
# stub stub_pvalue stub_marker
# <dbl> <dbl> <chr>
# 1 1 0 1***
# 2 2 0.04 2
# 3 3 0.07 3
# 4 4 0.2 4
But if we want to pass multiple columns to this function, we need to use purrr
:
#sample data with multiple sets of columns:
df <- tibble(stub = c(1,2,3,4),
stub_pvalue = c(.00, .04, .07,.2),
sho = c(8,7,6,5),
sho_pvalue = c(.005, .03, .00,.24))
library(purrr)
pmap_dfc(list(c("stub", "sho")), ~ assign_stars(df, ..1, 0.01, "***")) %>%
select(!! names(df), ends_with("marker"))
#> # A tibble: 4 x 6
#> stub stub_pvalue sho sho_pvalue stub_marker sho_marker
#> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
#> 1 1 0 8 0.005 1*** 8***
#> 2 2 0.04 7 0.03 2 7
#> 3 3 0.07 6 0 3 6***
#> 4 4 0.2 5 0.24 4 5
We can also use different threshold
and marker
for each column:
library(purrr)
pmap_dfc(list(c("stub", "sho"), c(0.01, 0.04), c("*", "**")),
~ assign_stars(df, ..1, ..2, ..3)) %>%
select(!! names(df), ends_with("marker"))
#> # A tibble: 4 x 6
#> stub stub_pvalue sho sho_pvalue stub_marker sho_marker
#> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
#> 1 1 0 8 0.005 1* 8**
#> 2 2 0.04 7 0.03 2 7**
#> 3 3 0.07 6 0 3 6**
#> 4 4 0.2 5 0.24 4 5