0

I have the following data frame:

library(tidyverse)
df <- structure(list(cell_type = c("Best4+ Enterocytes", "Cycling TA", 
"E.Absorptive"), marker_genes = c("OTOP3", "E2F7", "REN")), row.names = c(NA,  -3L), class = c("tbl_df", "tbl", "data.frame"))

It looks like this:

# A tibble: 3 x 2
  cell_type          marker_genes
  <chr>              <chr>       
1 Best4+ Enterocytes OTOP3       
2 Cycling TA         E2F7        
3 E.Absorptive       REN  

What I want to do is to lower case the string from marker_genes, but from second letter onwards, resulting in this (done by hand):

  cell_type          marker_genes  
1 Best4+ Enterocytes Otop3       
2 Cycling TA         E2f7        
3 E.Absorptive       Ren 

How can I achieve that?

littleworth
  • 4,781
  • 6
  • 42
  • 76

1 Answers1

3

There is a function in stringr for this

stringr::str_to_title(df$marker_genes)
#[1] "Otop3" "E2f7"  "Ren"  

Or the stringi equivalent

stringi::stri_trans_totitle(df$marker_genes)

To use it in pipes, we can do

library(dplyr)
df %>% mutate(marker_genes = stringr::str_to_title(marker_genes))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213