Nimzo
Nimzo

Reputation: 492

Add number of occurrences for each element of a character vector

Assuming we have

sig <- c("In neither", "In neither", "In both", 
         "T1 vs Ta", "T1 vs Ta",   "T1 vs Ta",  
         "T2 vs T1", "T2 vs T1", "T2 vs T1",  "T2 vs T1")

how could someone obtain the following two?

sig.size <- c("In neither (n = 2)", "In neither (n = 2)", "In both (n = 1)"
          "T1 vs Ta (n = 3)", "T1 vs Ta (n = 3)", "T1 vs Ta (n = 3)",  
          "T2 vs T1 (n = 4)", "T2 vs T1 (n = 4)", "T2 vs T1 (n = 4)",  "T2 vs T1 (n = 4)")

sig.fac <- c("T1 vs Ta (n = 3)", "T2 vs T1 (n = 4)", "In both (n = 1)", "In neither (n = 2)")

This is an excerpt of the data, since the vector sig contains more than 10k elements. I tried using table(sig) and a double for loop in combination with gsub and paste0 but wasn't able to get anything. The thing is that I would like to pass this automation in a function that generates scatterplots. For consistency reasons, I need sig.fac to have this specific ordering, so as to define factor levels before doing the ggplot. e.g. with:

df$sig <- factor(df$sig, levels=sig.fac)

Thanks in advance for the solution.

Upvotes: 1

Views: 38

Answers (3)

akrun
akrun

Reputation: 887108

We could use paste for this problem

paste0(names(sig.tbl), " (n= ", sig.tbl, ")")
#[1] "In both (n= 1)"    "In neither (n= 2)" "T1 vs Ta (n= 3)" 
#[4] "T2 vs T1 (n= 4)"

Or stack the table to a two column dataset and paste

with(stack(sig.tbl), paste0(ind, " (n = ", values, ")"))

data

sig <- c("In neither", "In neither", "In both", 
         "T1 vs Ta", "T1 vs Ta",   "T1 vs Ta",  
         "T2 vs T1", "T2 vs T1", "T2 vs T1",  "T2 vs T1")
sig.tbl <- table(sig)

Upvotes: 1

Edu
Edu

Reputation: 903

Alternatively, using a tidy approach

library(tibble)
library(dplyr)
library(glue)
df = tibble(sig = sig) %>% count(sig) %>% mutate(var = glue('{sig} (n = {n})'))
 unique(df$var)

[1] "In both (n = 1)"    "In neither (n = 2)" "T1 vs Ta (n = 3)"   "T2 vs T1 (n = 4)" 

Upvotes: 1

r2evans
r2evans

Reputation: 160437

sig <- c("In neither", "In neither", "In both", 
         "T1 vs Ta", "T1 vs Ta",   "T1 vs Ta",  
         "T2 vs T1", "T2 vs T1", "T2 vs T1",  "T2 vs T1")
sig.tbl <- table(sig)
sprintf("%s (n = %s)", sig, sig.tbl[sig])
#  [1] "In neither (n = 2)" "In neither (n = 2)" "In both (n = 1)"    "T1 vs Ta (n = 3)"   "T1 vs Ta (n = 3)"  
#  [6] "T1 vs Ta (n = 3)"   "T2 vs T1 (n = 4)"   "T2 vs T1 (n = 4)"   "T2 vs T1 (n = 4)"   "T2 vs T1 (n = 4)"  
sprintf("%s (n = %s)", names(sig.tbl), sig.tbl)
# [1] "In both (n = 1)"    "In neither (n = 2)" "T1 vs Ta (n = 3)"   "T2 vs T1 (n = 4)"  

Order can be handled many ways, here's one:

sig.tbl <- sig.tbl[match(names(sig.tbl), c("T1 vs Ta", "T2 vs T1", "In both", "In neither"))]
sprintf("%s (n = %s)", names(sig.tbl), sig.tbl)
# [1] "T1 vs Ta (n = 3)"   "T2 vs T1 (n = 4)"   "In both (n = 1)"    "In neither (n = 2)"

Upvotes: 1

Related Questions