HJain
HJain

Reputation: 145

Replace data in a column using R

I have a column in my data frame with data something like below:

RIsGreat
ILoveDataAnalysis
...
...

The labels are getting very long when plotting it using ggplot. Tried using str_wrap but that doesnt help because these are one-word labels.

Tried to replace RIsGreat with R Is Great using replace(x, "RIsGreat", "R Is Great). This doesn't work.

Can anyone please help.

Thanks in advance.

Upvotes: 0

Views: 62

Answers (2)

David Klotz
David Klotz

Reputation: 2431

library(snakecase)
library(dplyr)
Events <- c("RIsGreat", "ILoveDataAnalysis")

to_mixed_case(Events) %>% gsub("_"," ",.)

Upvotes: 1

HJain
HJain

Reputation: 145

Here is a regex that just inserts space between letter (any case) followed by capital letter. If you have other edge cases, can modify.

Events <- c("RIsGreat", "ILoveDataAnalysis")
gsub("([A-z])([A-Z])", "\\1 \\2", Events)
#> [1] "R Is Great"           "I Love Data Analysis"

Created on 2018-05-01 by the reprex package (v0.2.0).

Upvotes: 0

Related Questions