Changing name capitalization/format in column

I have a column (P1) in my data of names that is like this: BABACAR.THIOMBANE DAMEN.THACKER GABE.QUINNETT JAVARY.CHRISTMAS SCOTT.BLAKNEY

I want it to be transformed into: Babacar Thiombane | Damen Thacker | Gabe Quinnett | Javary Christmas | Scott Blakney

I know I need to use the tolower() function, but other than that I am very lost. New to R and will take any and all help please!

Do you like this way of doing things?

It would be helpful if you could create your own data for me next time.

library(tidyverse)

# your own data
df<- tibble::tibble(A=1,B=1,C=1,D=1,E=1)

names(df)<- c("BABACAR.THIOMBANE","DAMEN.THACKER",
      "GABE.QUINNETT","JAVARY.CHRISTMAS","SCOTT.BLAKNEY")
df

# change names
names(df)<- c("Babacar Thiombane", ......)

# use rename
df %>% rename("Babacar Thiombane"=BABACAR.THIOMBANE)

#snakecase lib
install.packages("snakecase")
df %>% 
  names() %>% 
  str_replace_all("\\.","-") %>% 
  snakecase::to_upper_camel_case(names(df)) %>% 
  str_replace_all("-"," ")

As you may know, it is not advisable to enter spaces or dot signs in the column names of R.

It can cause bugs.

The various packages of R do not support such column names.
Try using janitor::clean_names() to find out the best names.

Hmm I can't seem to work it out. I am trying to just remove the periods and change the capitalization in a column (P1) and the data is BABACAR.THIOMBANE. So i want the resulting column to be Babacar Thiombane. Sorry I am new to R

str_to_title() is the very right function for capitalization
but we should take punctuation mark(.) into account.
For general usage, I also include another case
such as "BABACAR.THIOMBANE.AAA" and "BABACAR".

Method 1

  1. split P1 by "."
  2. apply str_to_title() function using sapply() function
  3. concatenate separated words using "."

Method 2 : simple

  1. replace "." with " "
  2. apply str_to_title() function
  3. replace " " with "."
  4. concatenate separated words using "."

Hope this helps.

# R code
library(stringr) # str_to_title

df <- as.data.frame(
        c("BABACAR.THIOMBANE", "DAMEN.THACKER",
          "GABE.QUINNETT", "JAVARY.CHRISTMAS","SCOTT.BLAKNEY",
          "BABACAR.THIOMBANE.AAA","BABACAR"))
colnames(df) <- "P1"

df$p2 <- sapply( df$P1, function(x) { paste(str_to_title(
                scan(text = x, sep = ".", what = "")), collapse = ".")} )

df$p3 <- gsub(" ",".",str_to_title(gsub("[.]", " ", df$P1)))

df
# output
> df
                     P1                    p2                    p3
1     BABACAR.THIOMBANE     Babacar.Thiombane     Babacar.Thiombane
2         DAMEN.THACKER         Damen.Thacker         Damen.Thacker
3         GABE.QUINNETT         Gabe.Quinnett         Gabe.Quinnett
4      JAVARY.CHRISTMAS      Javary.Christmas      Javary.Christmas
5         SCOTT.BLAKNEY         Scott.Blakney         Scott.Blakney
6 BABACAR.THIOMBANE.AAA Babacar.Thiombane.Aaa Babacar.Thiombane.Aaa
7               BABACAR               Babacar               Babacar

Ok this is very helpful, thank you. For the sake of repeatability could you do it with P1 or something instead of listing the values in P1. The P1 column of the dataframe always comes out the same way [FIRSTNAME.LASTNAME] and want the process to be able to be repeated in different scripts.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.