Top_n function truncating values after decimal

I am using top_n function to find out top 10 machines based on usage hours per month,region and server but i am getting the usage hours truncated(no values after decimal,only the value before decimal and decimal point).

The sum_of_hours column is numeric in the dataframe.

top_10_by_month_region_server<-df3_machine_region%>%group_by(Month,Region,Server)%>%ungroup()%>%top_n(10,df3_machine_region$sum_as_hours)
top_10_by_month_region_server_sort<-top_10_by_month_region_server%>%arrange(desc(top_10_by_month_region_server$sum_as_hours))

Machine.Name Region Month Year Server sum_as_hours

1 AMS-L-94741.europe.shell.com Europe July 2020 AMSDC-1-S-90588 1895.
2 AMS-L-95267.europe.shell.com Europe August 2020 AMSDC-1-S-90588 1471.
3 PHC-D-91943.africa-me.shell.com Africa January 2020 AMSDC-1-S-90588 1156.
4 SERHO-L-84510.asia-pac.shell.com APAC July 2020 PEJJBT-S-8008 734.
5 SERHO-L-91604.asia-pac.shell.com APAC August 2020 PEJJBT-S-8008 740.
6 STCB-L-97240.asia-pac.shell.com APAC August 2020 PEJJBT-S-8008 740.
7 STCB-L-97779.asia-pac.shell.com APAC August 2020 PEJJBT-S-8008 740.
8 W180402.corp.pdo.om PDO July 2020 AMSDC-1-S-90588 951.
9 W180402.corp.pdo.om PDO August 2020 AMSDC-1-S-90588 1053.
10 WDLVMMPLDS1.d2kbinz.local Americas August 2020 HOUCY1-S-07400 740.

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

The values which i have are as follows prior to using top_n function:

Machine.Name Region Month Year Server sum_as_hours

1 AMS-L-94741.europe.shell.com Europe July 2020 AMSDC-1-S-90588 1895.50
2 AMS-L-95267.europe.shell.com Europe August 2020 AMSDC-1-S-90588 1471.20
3 PHC-D-91943.africa-me.shell.com Africa January 2020 AMSDC-1-S-90588 1156.567
4 SERHO-L-84510.asia-pac.shell.com APAC July 2020 PEJJBT-S-8008 734.234
5 SERHO-L-91604.asia-pac.shell.com APAC August 2020 PEJJBT-S-8008 740.3
6 STCB-L-97240.asia-pac.shell.com APAC August 2020 PEJJBT-S-8008 740.568
7 STCB-L-97779.asia-pac.shell.com APAC August 2020 PEJJBT-S-8008 740.632
8 W180402.corp.pdo.om PDO July 2020 AMSDC-1-S-90588 951.245
9 W180402.corp.pdo.om PDO August 2020 AMSDC-1-S-90588 1053.42
10 WDLVMMPLDS1.d2kbinz.local Americas August 2020 HOUCY1-S-07400 740.53

after using top_n function as below:

top_10_by_month_region_server<-df3_machine_region%>%group_by(Month,Region,Server)%>%ungroup()%>%top_n(10,df3_machine_region$sum_as_hours)
top_10_by_month_region_server_sort<-top_10_by_month_region_server%>%arrange(desc(top_10_by_month_region_server$sum_as_hours))

The output is as follows:

Machine.Name Region Month Year Server sum_as_hours

1 AMS-L-94741.europe.shell.com Europe July 2020 AMSDC-1-S-90588 1895.
2 AMS-L-95267.europe.shell.com Europe August 2020 AMSDC-1-S-90588 1471.
3 PHC-D-91943.africa-me.shell.com Africa January 2020 AMSDC-1-S-90588 1156.
4 SERHO-L-84510.asia-pac.shell.com APAC July 2020 PEJJBT-S-8008 734.
5 SERHO-L-91604.asia-pac.shell.com APAC August 2020 PEJJBT-S-8008 740.
6 STCB-L-97240.asia-pac.shell.com APAC August 2020 PEJJBT-S-8008 740.
7 STCB-L-97779.asia-pac.shell.com APAC August 2020 PEJJBT-S-8008 740.
8 W180402.corp.pdo.om PDO July 2020 AMSDC-1-S-90588 951.
9 W180402.corp.pdo.om PDO August 2020 AMSDC-1-S-90588 1053.
10 WDLVMMPLDS1.d2kbinz.local Americas August 2020 HOUCY1-S-07400 740.

So if you see above,the values after decimal are getting truncated for sum_as_hours column after using top_n function

Hello again. I see that you repeated your example, unfortunately this doesn't meet the criteria of a reprex (reproducible example) as explained in the link I shared.
The intention of sharing this information to you and having you post examples of the required standards is primarily to increase the likelihood that you receive the help you seek , both now and in the future. :slight_smile:

The values are not actually getting truncated, they are just displayed with fewer decimal places for presentation purposes, the actual values are still with full precision, having said that, I think you should check your code since it seems it has several issues.

An example just to illustrate

library(dplyr)

sample_df <- data.frame(
  stringsAsFactors = FALSE,
      Machine.Name = c("AMS-L-94741.europe.shell.com",
                       "AMS-L-95267.europe.shell.com",
                       "PHC-D-91943.africa-me.shell.com","SERHO-L-84510.asia-pac.shell.com",
                       "SERHO-L-91604.asia-pac.shell.com",
                       "STCB-L-97240.asia-pac.shell.com","STCB-L-97779.asia-pac.shell.com",
                       "W180402.corp.pdo.om","W180402.corp.pdo.om","WDLVMMPLDS1.d2kbinz.local"),
            Region = c("Europe","Europe","Africa",
                       "APAC","APAC","APAC","APAC","PDO","PDO","Americas"),
             Month = c("July","August","January",
                       "July","August","August","August","July","August",
                       "August"),
              Year = c(2020,2020,2020,2020,2020,
                       2020,2020,2020,2020,2020),
            Server = c("AMSDC-1-S-90588",
                       "AMSDC-1-S-90588","AMSDC-1-S-90588","PEJJBT-S-8008",
                       "PEJJBT-S-8008","PEJJBT-S-8008","PEJJBT-S-8008","AMSDC-1-S-90588",
                       "AMSDC-1-S-90588","HOUCY1-S-07400"),
      sum_as_hours = c(1895.5,1471.2,1156.567,
                       734.234,740.3,740.568,740.632,951.245,1053.42,740.53)
)

new_df <- sample_df %>% 
    group_by(Month,Region,Server) %>% 
    top_n(10, sum_as_hours)

new_df$sum_as_hours
#>  [1] 1895.500 1471.200 1156.567  734.234  740.300  740.568  740.632  951.245
#>  [9] 1053.420  740.530

Created on 2020-09-29 by the reprex package (v0.3.0)

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.