How to change capitalization and add space to data in variable

I want to change the values in the status column changing KOBE.WILLIAMSON On to Kobe Williamson. So just change the capitalization and add a space. I know thats relatively simple, but new to R and not sure how to do that.

Status Mins oMins dMins
KOBE.WILLIAMSON On 299.366 153.684 145.683
KOBE.WILLIAMSON Off 89.483 45.196 44.286

I believe the best option here is to use tocamel function from rapportools package. It's as simple as:

# df <- tibble(Status = c("KOBE.WILLIAMSON On", "KOBE.WILLIAMSON Off"))
df %>%
    mutate(
        Status = tolower(Status),
        Status = tocamel(Status, upper = T, sep = " ")
    )

Note that we need to lower the whole string at first, cause it just capitalises the first letter of each word.

If you don't want to add unnecessary dependencies, there are many good suggestions across the internet. Good luck!

If you're using the tidyverse, stringr has some very useful functions, str_replace_all and str_to_title:

library(dplyr)
library(stringr)
df %>% mutate(Status = str_to_title(str_replace_all(Status, "\\.", " ")))
1 Like
suppressPackageStartupMessages({
  library(dplyr)
  library(stringr)
})

dat <- data.frame(
  Status =c("KOBE.WILLIAMSON On", "KOBE.WILLIAMSON Off"),
  Mins = c(299.366, 89.483),
  oMins = c(153.684, 45.196),
  dMins = c(145.683,44.286))

period <- "\\."
blank  <- " "

dat %>% 
  mutate(Status = str_replace(Status,period,blank),
         Status = str_to_title(Status))
#>                Status    Mins   oMins   dMins
#> 1  Kobe Williamson On 299.366 153.684 145.683
#> 2 Kobe Williamson Off  89.483  45.196  44.286
1 Like

Ok that worked, thank you so much! I am having one problem when I am trying to view it though. It runs on the function correctly, but gives me an error when I try to view the same code. Sorry if it's not the cleanest most efficient code

on_off_generator("KOBE.WILLIAMSON", decstats, Included = ("DARRION.TRAMMELL")) %>%
  select(Status, Mins, oMins, dMins, oPOSS, dPOSS, ORTG, DRTG, NETRTG) %>%
  mutate(Status = tolower(Status), Status = tocamel(Status, upper = T, sep = " "))
View(on_off_generator("KOBE.WILLIAMSON", decstats, Included = ("DARRION.TRAMMELL"))) %>%
  select(Status, Mins, oMins, dMins, oPOSS, dPOSS, ORTG, DRTG, NETRTG) %>%
  mutate(Status = tolower(Status), Status = tocamel(Status, upper = T, sep = " "))
> on_off_generator("KOBE.WILLIAMSON", decstats, Included = ("DARRION.TRAMMELL")) %>%
+   select(Status, Mins, oMins, dMins, oPOSS, dPOSS, ORTG, DRTG, NETRTG) %>%
+   mutate(Status = tolower(Status), Status = tocamel(Status, upper = T, sep = " "))
               Status    Mins   oMins   dMins oPOSS dPOSS    ORTG   DRTG NETRTG
1  Kobe Williamson On 299.366 153.684 145.683   535   534  97.009 92.322  4.687
2 Kobe Williamson Off  89.483  45.196  44.286   184   178 111.957 93.820 18.136
> View(on_off_generator("KOBE.WILLIAMSON", decstats, Included = ("DARRION.TRAMMELL"))) %>%
+   select(Status, Mins, oMins, dMins, oPOSS, dPOSS, ORTG, DRTG, NETRTG) %>%
+   mutate(Status = tolower(Status), Status = tocamel(Status, upper = T, sep = " "))
Error in UseMethod("select") : 
  no applicable method for 'select' applied to an object of class "NULL"

You're trying to apply select function on a View result. It doesn't work.
You either should apply View to the whole code there (watch the parentheses) or just add %>% View() at the end.

1 Like

Ok thank you so much! That worked. Do you have any suggestions on how to learn R better other than google and trial and error?

I really truly believe you should try swirl. It's a great start to learn R interactively inside R itself.

1 Like

Ok I will try it out! Thank you. I am no getting an error in my rename function. Do you see anything glaringly wrong with it?

on_off_generator("KOBE.WILLIAMSON", decstats, Included = ("DARRION.TRAMMELL")) %>%
  select(Status, Mins, oMins, dMins, oPOSS, dPOSS, ORTG, DRTG, NETRTG) %>%
  mutate(Status = tolower(Status), Status = tocamel(Status, upper = T, sep = " ")) %>%
  rename("Net Rating" = NETRTG, "Off Rating" = ORTG, "Def Rating" = DRTG) %>%

Error in rename(., Net Rating = NETRTG, Off Rating = ORTG, Def Rating = DRTG) :
unused arguments (Net Rating = NETRTG, Off Rating = ORTG, Def Rating = DRTG)

Look at the rename function examples.
You can't assign column to a string. If you want a name with the space in it you should use

rename(`Net Rating` = NETRTG, `Off Rating` = ORTG, `Def Rating` = DRTG)

UPD

I have looked at those examples before and have tried to use them as reference. I keep getting unused argument no matter how I move it around.

df1a=data.frame(on_off_generator("EDDIE.DAVIS", decstats, Included = ("AHMED.ALI"))) %>%
  select(Status, Mins, ORTG, DRTG, NETRTG) %>%
  mutate_if(is.numeric, round) %>%
  rename('Net_Rating' = NETRTG, 'Off Rating' = ORTG, 'Def Rating' = DRTG)

Here is the error and I am dumbfounded at how to change it.

Error in rename(., Net Rating = NETRTG, Off Rating = ORTG, Def Rating = DRTG) :
unused arguments (Net Rating = NETRTG, Off Rating = ORTG, Def Rating = DRTG)

You've used just a single quote. Result is the same - the expression in single or double quotes is a string.
In my answer I've used backquote: `

This topic was automatically closed 7 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.