Home > Net >  Remove the specific digits from numbers and keep in data frame
Remove the specific digits from numbers and keep in data frame

Time:01-06

suppose I have following data frame:

df<- data_frame(id= c(001200567, 013009887, 014009344, 015009875,016004556),cost = (10.5:14.5))

for column id I want just to keep digit 2 and 3. I have tried df <- substr(df$id,2,3) bu it does not work. expected output:

     id  cost
 
1     1  10.5
2    13  11.5
3    14  12.5
4    15  13.5
5    16  14.5

CodePudding user response:

When id is a character, we have

df<- data_frame(id= c("001200567", "013009887", "014009344", "015009875","016004556"),cost = (10.5:14.5))

Using substr(df$id,2,3) we would get 01 instead of 1 for the first entry, however we can remove the trailing 0 by making it numeric and then a character again:

df$id <- as.character(as.numeric(substr(df$id,2,3)))

This gives:

> df
# A tibble: 5 x 2
  id     cost
  <chr> <dbl>
1 1      10.5
2 13     11.5
3 14     12.5
4 15     13.5
5 16     14.5

CodePudding user response:

Another possible solution, based on tidyverse (stringr::str_sub allows to extract sub-strings, given their start and end positions):

library(tidyverse)

df <- data.frame(id= c("001200567", "013009887", "014009344", "015009875","016004556"),cost = (10.5:14.5))

df %>% 
  mutate(id = str_sub(id, 2, 3)) %>% 
  type.convert(as.is = TRUE)

#>   id cost
#> 1  1 10.5
#> 2 13 11.5
#> 3 14 12.5
#> 4 15 13.5
#> 5 16 14.5
  •  Tags:  
  • Related