0 votes
1 view
in R Programming by (43.2k points)

I want to calculate to the first decimal place using the integer numbers but whatever I do, it is rounded and does not give me the precision I want. I have implemented the below code:

type = c('A', 'B', 'C', 'D', 'C', 'B')

count = c(1, 4, 8, 4, 2, 4)

df1 = data.frame(type, count)

type2 = c('A', 'B', 'C', 'D', 'B', 'C', 'D', 'A')

count2 = c(3, 7, 1, 4, 8, 4, 5, 2)

df2 = data.frame(type2, count2)

sum1 <- tapply(df1$count, type, sum)

sum2 <- tapply(df2$count2, type2, sum)

round(sum1/sum2*100.0, 1) # Want to calculate it to the 1st decimal

I get this:

A   B   C   D 

20  53 200  44

and I want this:

   A     B      C     D 

20.0  53.3  200.0  44.4

1 Answer

0 votes
by (92.8k points)

See by increasing the number of digits only you'll see the impact of the round. Here in the below code, the digits denote the number of significant digits to be displayed.

options(digits = 10)

round(sum1/sum2*100, 1)

#    A     B     C     D 

# 20.0  53.3 200.0  44.4 

round(sum1/sum2*100, 2)

#    A      B      C      D 

# 20.00  53.33 200.00  44.44 

If you are interested in R certification, then do refer to the R programming certification

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...