Home > Back-end >  How to remove the hyphens and the middle numbers in R
How to remove the hyphens and the middle numbers in R

Time:12-23

Here is my data:

df<-read.table (text=" ID   Range

1   1-2-3
2   1-2-3
3   4-5-6
4   4-5-7
5   10-11-12
6   10-11-12
7   14-16-17
8   19-20-21
9   10-11-12
10  12-13-14

", header=TRUE)

I want to get the following table.

  

  ID    Range
    1   1-3
    2   1-3
    3   4-6
    4   4-7
    5   10-12
    6   10-12
    7   14-17
    8   19-21
    9   10-12
    10  12-14

The logic is to remove the middle numbers and keep only one hyphen.

It seems this script does not work

 gsub("-", "", df$Range)

CodePudding user response:

We could use str_replace with regex \\- for the hyphen and \\d for digits after the hyphen:

library(dplyr)
library(stringr)

df %>% 
  mutate(Range= str_replace(Range, '\\-\\d ', ''))


   ID Range
1   1   1-3
2   2   1-3
3   3   4-6
4   4   4-7
5   5 10-12
6   6 10-12
7   7 14-17
8   8 19-21
9   9 10-12
10 10 12-14

CodePudding user response:

In base R, we can use sub

df$Range <- sub("-\\d ", "", df$Range)

-output

> df
   ID Range
1   1   1-3
2   2   1-3
3   3   4-6
4   4   4-7
5   5 10-12
6   6 10-12
7   7 14-17
8   8 19-21
9   9 10-12
10 10 12-14
  •  Tags:  
  • r
  • Related