dataframe - R: How to uppercase first letter of each word split by semicolon in data frame column? -
suppose have dataframe df
.
> df <- data.frame(disease = c('disease entry1; disease entry2', 'disease entry4','disease entry5; disease entry6'), id = c(1,2,3)) > df disease id 1 disease entry1; disease entry2 1 2 disease entry4 2 3 disease entry5; disease entry6 3
how can manipulate each disease entry lowercase except first letter each entry? i.e.
> df disease id 1 disease entry1; disease entry2 1 2 disease entry4 2 3 disease entry5; disease entry6 3
i assume take use of tolower
function somehow, how take account semicolons?
you can firstly convert letters lower case, , use gsub
convert letters @ bos or after ;
upper case using reference \\1
\\u
:
df$disease <- gsub("(?<=^|; )([a-z])", "\\u\\1", tolower(df$disease), perl = t) df # disease id #1 disease entry1; disease entry2 1 #2 disease entry4 2 #3 disease entry5; disease entry6 3
Comments
Post a Comment