0

I have a data.table that looks like this:

require(data.table)
require(stringi)

DT <- data.table(ID = c(1,2,3), Name =c("john peter", "joe", "Ann cathrine"))

I would like to manipulate the Name column such that I upper the first character of names AND the middlenames too:

data.table(ID = c(1,2,3), Name =c("John Peter", "Joe", "Ann Cathrine"))

My Approach is as follows:

  • split the Name string with " " as seperator
  • upper first character of each of the elements of the list resulting from split
  • paste the manipulated list elements together using paste with collapse.

I tried this with both do.call and lapply using the user defined function toUpperFirst:

DT[,  NameCapFirstTry   := paste0(do.call(toUpperFirst, stri_split_fixed(Name," ")), collapse = ' ')]

DT[,  NameCapSecondTry  := paste0(lapply(stri_split_fixed(Name," "),  toUpperFirst), collapse = ' ')]

toUpperFirst <- function (x){
  return (paste0(toupper(substring(x,1,1)), tolower(substring(x,2))))
}

I don't quite understand what goes wrong. Obviously, the split takes the whole column instead of applying it vector-wise. Any idea how to fix this?

HannesZ
  • 409
  • 2
  • 5
  • 11

0 Answers0