How to stop default printing of large numbers in R?

R ProgrammingServer Side ProgrammingProgramming

When we write a long number in R, by default the printed output of that number is in scientific notation. To stop this printing behavior we can use sprint function.

For example, if we have a data frame called df that contains a column say X having long numbers then we can stop printing of these numbers in scientific notation by using the below given command −

df$X<-sprintf("%f",df$X)

Example 1

Following snippet creates a sample data frame −

x<-rpois(20,100000000000000)
df1<-data.frame(x)
df1

Output

The following dataframe is created −

   x
1  NA
2  NA
3  NA
4  NA
5  NA
6  NA
7  NA
8  NA
9  NA
10 NA
11 NA
12 NA
13 NA
14 NA
15 NA
16 NA
17 NA
18 NA
19 NA
20 NA

To print values of x with all digits, add the following code to the above snippet −

df1$x<-sprintf("%f",df1$x)
df1

Output

If you execute all the above given codes as a single program, it generates the following output −

            x
1  100000002878004.000000
2  100000006073034.000000
3  100000015081157.000000
4  99999991070780.000000
5  100000001649055.000000
6  100000008293503.000000
7  100000002065917.000000
8  100000001116032.000000
9  99999995600778.000000
10 100000003840096.000000
11 99999989811240.000000
12 99999993682758.000000
13 99999987127237.000000
14 100000004413443.000000
15 99999997385728.000000
16 100000004964710.000000
17 99999987378461.000000
18 100000013440743.000000
19 100000005847206.000000
20 100000019558343.000000

Example 2

Following snippet creates a sample data frame −

y<-rpois(20,123574125541235)
df2<-data.frame(y)
df2

Output

The following dataframe is created −

   y
1  NA
2  NA
3  NA
4  NA
5  NA
6  NA
7  NA
8  NA
9  NA
10 NA
11 NA
12 NA
13 NA
14 NA
15 NA
16 NA
17 NA
18 NA
19 NA
20 NA

To print values of y with all digits, add the following code to the above snippet −

df2$y<-sprintf("%f",df2$y)
df2

Output

If you execute all the above given codes as a single program, it generates the following output −

      y
1  123574119946324.000000
2  123574123392734.000000
3  123574141533986.000000
4  123574126127884.000000
5  123574127737879.000000
6  123574117603698.000000
7  123574125442109.000000
8  123574128811340.000000
9  123574111352873.000000
10 123574117712696.000000
11 123574129555656.000000
12 123574123416637.000000
13 123574129492703.000000
14 123574127602845.000000
15 123574147713179.000000
16 123574133125007.000000
17 123574131515857.000000
18 123574128446719.000000
19 123574116541207.000000
20 123574133423019.000000
raja
Published on 06-Nov-2021 07:29:35
Advertisements