0

I want to exclude 0 after decimal point from my summary below like if a value is

22.34
31.7
76.00

output should be

22.34
31.7
76

if there is zero after decimal point it should get excluded automatically

Median =round(quantile(data$column, type=4, probs = seq(0, 1, 0.25), na.rm=TRUE)

2 Answers2

4

Using prettyNum.

dat$V1 <- prettyNum(dat$V1)

dat
#      V1
# 1 22.34
# 2  31.7
# 3    76

Data:

dat <- read.table(text="22.34
31.7
76.00")
jay.sf
  • 60,139
  • 8
  • 53
  • 110
1

You can do this using regex :

x <- c(22.34, 31.7, 76.00, 75.10100)
sub('\\.0*$', '', x)
#[1] "22.34"  "31.7"   "76"     "75.101"

Here we remove 0's occurring at the end of the number after decimal places.


To apply this for multiple columns, we can use across

library(dplyr)
new_Tab <- summ_tab1 %>% mutate(across(everything(), ~sub('\\.0*$', '', .)))

Or in earlier version of dplyr use mutate_all :

new_Tab <- summ_tab1 %>% mutate_all(~sub('\\.0*$', '', .))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213