I want to delete the ":00" in the column 'female_male_ratio'.
Is there any code suggestions on how to do that ?
I want to delete the ":00" in the column 'female_male_ratio'.
Is there any code suggestions on how to do that ?
We can accomplish this by using mutate
from the dplyr
package and str_remove
from the stringr
package with the regular expression
":00$"
to ensure it's a :00
prior to the end of the string and not in the middle like in 46:00:55
. Note, I am only using the female_male_ratio
variable since you did not provide a proper reproducible example.
library(tibble)
library(dplyr)
library(stringr)
df <- tibble(
female_male_ratio = c("33: 67",
"42:58:00",
"46:54:00",
NA,
"37 : 63",
"45:55:00",
"46:54:00")
)
df_remove <- df %>%
mutate(
female_male_ratio = str_remove(female_male_ratio, ":00$")
)
# A tibble: 7 x 1
female_male_ratio
<chr>
1 33: 67
2 42:58
3 46:54
4 NA
5 37 : 63
6 45:55
7 46:54
If you want to get rid of the whitespace
in just the female_male_ratio
variable, you can use sub
from base
R.
df_remove$female_male_ratio <- sub("\\s+", "", df_remove$female_male_ratio)
# A tibble: 7 x 1
female_male_ratio
<chr>
1 33:67
2 42:58
3 46:54
4 NA
5 37:63
6 45:55
7 46:54
Created on 2020-11-15 by the reprex package (v0.3.0)