rlapplygsubcharacter-replacement

Changing the Values with Specific Character from Data Frame in R


My data frame like this.

    X           Y              Z 
    10.5 m³/s   15. m³/s    14.3 m³/s
    10. m³/s    11. 5m³/s    16.7 m³/s
    10.8 m³/s   15.7 m³/s    1.5 m³/s

I have to delete some specific characters from my data frame. In order to overwhelm this issue i have wrote some code like this.

  df[] <- lapply(df,gsub, pattern='. m³/s', replacement='')

It seems to be worked. But i encountered with "." issue which is orginally being shape of the data.

X      Y       Z 
10.5   15.     14.3
10.    11.5    16.7
10.8   15.7    1.5

As well as I did every combination for the pattern function.

I need double values so i have to delete points just where placing at end of the values.

Thanks for helps.


Solution

  • You can account for the decimal in the whole number values by adding a regex \\.?, which matches literal "." if present one or zero times.

    d <- data.frame(X = "10.5 m³/s", Y = "15. m³/s", Z = "14.3 m³/s")
    lapply(d, gsub, pattern='\\.? m³/s', replacement='')
    
    $X
    [1] "10.5"
    
    $Y
    [1] "15"
    
    $Z
    [1] "14.3"