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'.
CodePudding user response:
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
CodePudding user response:
Replace "T" with "0.0" and "s" with "" using str_replace
:
a <- c(0.00, 0.06, "T", "0.02s")
stringr::str_replace(a, c("T","s"), c("0.0",""))
#> [1] "0" "0.06" "0.0" "0.02"