Search code examples
rdata-cleaning

Data cleaning using R : use str_remove() function


I have a problem in cleaning my data using R. My dataframe looks like this; enter image description here

I need to clean the new_sub_weather$HOURLYPrecip column 1: Change 'T' value to '0.0' 2: Remove data value which has 's' at the back such as '0.02s'

My coding is like this;

new_sub_weather['HOURLYPrecip'][new_sub_weather['HOURLYPrecip'] == 'T'] <- '0.0'
str_remove(new_sub_weather$HOURLYPrecip, "s$")
unique(new_sub_weather$HOURLYPrecip) 

But I didn't manage to remove the character 's'.


Solution

  • You may use readr::parse_number which will automatically remove "s" from "0.02s" and turn the values to numeric.

    new_sub_weather$HOURLYPrecip[new_sub_weather$HOURLYPrecip == "T"] <- 0
    new_sub_weather$HOURLYPrecip <- readr::parse_number(new_sub_weather$HOURLYPrecip)
    

    For eg -

    x <- c(0, 12.1, '1.2s', '0.0', '0.02s')
    readr::parse_number(x)
    #[1]  0.00 12.10  1.20  0.00  0.02