Dear all,

I just wanted to know if there is a way to find out last digit or first digit in a numeric values using R? like the one we have with LEFT, RIGHT functions in excel?

thank you for your reply!

BR, M

Dear all,

I just wanted to know if there is a way to find out last digit or first digit in a numeric values using R? like the one we have with LEFT, RIGHT functions in excel?

thank you for your reply!

BR, M

Hi @kmmukesh, welcome to RStudio Community.

The `stringr`

package has functions that can help you accomplish this. Here's an illustration using the built-in `mtcars`

data set.

```
library(stringr)
mtcars$disp
#> [1] 160.0 160.0 108.0 258.0 360.0 225.0 360.0 146.7 140.8 167.6 167.6 275.8
#> [13] 275.8 275.8 472.0 460.0 440.0 78.7 75.7 71.1 120.1 318.0 304.0 350.0
#> [25] 400.0 79.0 120.3 95.1 351.0 145.0 301.0 121.0
# Get first digit of the disp variable for each car.
str_extract(mtcars$disp, "^\\d")
#> [1] "1" "1" "1" "2" "3" "2" "3" "1" "1" "1" "1" "2" "2" "2" "4" "4" "4" "7" "7"
#> [20] "7" "1" "3" "3" "3" "4" "7" "1" "9" "3" "1" "3" "1"
# Get last integer digit of the disp variable for each car.
str_extract(mtcars$disp, "\\d$")
#> [1] "0" "0" "8" "8" "0" "5" "0" "7" "8" "6" "6" "8" "8" "8" "2" "0" "0" "7" "7"
#> [20] "1" "1" "8" "4" "0" "0" "9" "3" "1" "1" "5" "1" "1"
# Get last decimal digit of the disp variable for each car.
str_match(mtcars$disp, "\\.(\\d$)")[, 2]
#> [1] NA NA NA NA NA NA NA "7" "8" "6" "6" "8" "8" "8" NA NA NA "7" "7"
#> [20] "1" "1" NA NA NA NA NA "3" "1" NA NA NA NA
# An alternate approach that is similar to Excel's LEFT and RIGHT functions.
# Get first digit of the disp variable for each car.
str_sub(mtcars$disp, start = 1L, end = 1L)
#> [1] "1" "1" "1" "2" "3" "2" "3" "1" "1" "1" "1" "2" "2" "2" "4" "4" "4" "7" "7"
#> [20] "7" "1" "3" "3" "3" "4" "7" "1" "9" "3" "1" "3" "1"
# Get last integer digit of the disp variable for each car (only works correctly
# if each number is of the same length).
str_sub(mtcars$disp, start = 3L, end = 3L)
#> [1] "0" "0" "8" "8" "0" "5" "0" "6" "0" "7" "7" "5" "5" "5" "2" "0" "0" "." "."
#> [20] "." "0" "8" "4" "0" "0" "" "0" "." "1" "5" "1" "1"
# Get last decimal digit of the disp variable for each car (only works correctly
# if each number is of the same length).
str_sub(mtcars$disp, start = 5L, end = 5L)
#> [1] "" "" "" "" "" "" "" "7" "8" "6" "6" "8" "8" "8" "" "" "" "" ""
#> [20] "" "1" "" "" "" "" "" "3" "" "" "" "" ""
```

^{Created on 2020-03-23 by the reprex package (v0.3.0)}

Thanks, Sid. It helps!

Great! If I solved your query, would you mind marking my post as a solution?

Sure, I will. Can I ask you one more leading question-

I have decimal values like 103.5, 36.8, 125.8- When I used a str_extract function, I am getting the last digit like "5", "8", "8". Is there a workaround to find to find out the decimal point as well like "0.5", "0.8", "0.8"

Thanks,

M

**EDIT:** I've updated my original post with an alternate way of doing this which is similar to how you would use the LEFT and RIGHT functions in Excel. Added a solution for finding the last decimal digit as well.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.