Home > Enterprise >  Changing the Values with Specific Character from Data Frame in R
Changing the Values with Specific Character from Data Frame in R

Time:05-19

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.

CodePudding user response:

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"

CodePudding user response:

Maybe readr::parse_number() is also an option?

library(dplyr)
df %>% mutate(across(everything(), readr::parse_number))

Result:

# A tibble: 3 x 3
      X     Y     Z
  <dbl> <dbl> <dbl>
1  10.5  15    14.3
2  10    11    16.7
3  10.8  15.7   1.5

CodePudding user response:

library(tidyverse)

df %>% mutate(across(X:Z,~as.double(str_remove_all(.x, " |m³/s"))))

Output:

# A tibble: 3 × 3
      X     Y     Z
  <dbl> <dbl> <dbl>
1  10.5  15    14.3
2  10    11.5  16.7
3  10.8  15.7   1.5

Input:

structure(list(X = c("10.5 m³/s", "10. m³/s", "10.8 m³/s"), 
    Y = c("15. m³/s", "11. 5m³/s", "15.7 m³/s"), Z = c("14.3 m³/s", 
    "16.7 m³/s", "1.5 m³/s")), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -3L))
  • Related