2

I want to delete the ":00" in the column 'female_male_ratio'.

dataset

Is there any code suggestions on how to do that ?

ekoam
  • 8,549
  • 1
  • 8
  • 22
  • just do `sub(':00$', '', dat$female_male_ration)` – onyambu Nov 15 '20 at 14:53
  • @Onyambu i did tht but when i print the dataset it does not change – Huvinesh Rajendran Nov 15 '20 at 14:58
  • Because you have to store it back into the dataset. ie `dat$female_male_ratio – onyambu Nov 15 '20 at 15:04
  • In order for us to help you, please provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). For example, to produce a minimal data set, you can use `head()`, `subset()`, or the indices. Then use `dput()` to give us something that can be put in R immediately. Also, please make sure you know what to do [when someone answers your question](https://stackoverflow.com/help/someone-answers). More info can be found at Stack Overflow's [help center](https://stackoverflow.com/help). Thank you! – iamericfletcher Nov 15 '20 at 15:30

1 Answers1

1

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)

iamericfletcher
  • 2,529
  • 6
  • 17