0

The R head() and top_n functions are removing decimals from two of my numeric columns and I cannot figure out why?

I am using R 3.4.4 with RStudio 1.2.5 and dplyr . I have got a dataset with 4 numeric columns along with four other types of columns. I am trying to grab the top ten rows from this dataset using a simple head(10) like so

Top_Ten_Drug_Subclass %>% 
  head(10)

One of the numeric column's decimals are removed. For ex - Instead of 25.63, it shows 25.The second column shows only 1 decimal (ex - 25.6) whereas the other two do show two decimal points after the number which is what I need. I am just very confused on why would a head() or a top_n() function will change the formats. May be explaining the below code will help solve this mystery. If I just run the below code through "ungroup ()" it gives me the proper formatting for allowed_amount column. As soon as I use head() or top_n() the formatting for Allowed_Amount is returned as an integer, however, the underlying type is still numeric/double. Now that is weird.

    All_Rx_Data_201907 %>% 
  filter(PLAN_ID %like% 'xxxxx' & `MONTH/YEAR` == '062019') %>% 
  inner_join(MemCountPerDrugClass, by = "DRUG_SUBCLASS") %>% 
  select(`MONTH/YEAR`, DRUG_SUBCLASS, DRUG_SUBCLASS_NAME, SCRIP_COUNT, ALLOWED_AMOUNT, PAID_AMOUNT, MEMBER_NUM) %>%
  group_by(`MONTH/YEAR`, DRUG_SUBCLASS, DRUG_SUBCLASS_NAME, MEMBER_NUM) %>% 
  summarise(SCRIP_COUNT = sum(SCRIP_COUNT), ALLOWED_AMOUNT = sum(ALLOWED_AMOUNT), PAID_AMOUNT = sum(PAID_AMOUNT)) %>% 
  mutate(Allowed_Cost_Per_Member = round((ALLOWED_AMOUNT/MEMBER_NUM), 2)
         , Allowed_Cost_Per_Script = round((ALLOWED_AMOUNT/SCRIP_COUNT), 2)) %>% 
  arrange(desc(ALLOWED_AMOUNT)) %>% 
  ungroup() %>% 
  top_n(10, ALLOWED_AMOUNT) %>% 
  arrange(desc(Allowed_Cost_Per_Script))

Has anyone come across such an issue? Thank you!

  • [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. That includes a sample of data, all necessary code, and a clear explanation of what you're trying to do and what hasn't worked. – camille Sep 28 '19 at 02:13
  • Add `%>% data.frame()` at the end of the chain and check. `Top_Ten_Drug_Subclass %>% head(10) %>% data.frame` – Ronak Shah Sep 28 '19 at 04:11
  • @Ronak Shah - Thank you for taking the time to help with my question. Data.frame is deprecated so that did not help. The class of Top_Ten_Drug_Subclass is 'tbl_df' already. – user12133051 Sep 29 '19 at 16:46

1 Answers1

0

Head() should be returning data without converting any values. Have you tried confirming the data types of your data set? To confirm your data hadn't been ingested as an integer causing the decimals to disappear. You can use the str(Top_Ten_Drug_Subclass) function.

Int is integer and num is numeric.

Example: 

'data.frame':   2 obs. of  3 variables:
 $ N  : int  1 2
 $ Age : num  21.5 15.0
 $ Name: Factor w/ 2 levels "Dora","John": 2 1
evng
  • 1
  • 1
  • I'm not sure how this answers the question – camille Sep 28 '19 at 02:13
  • @evng - Thank you for taking the time to answer my question. I have updated my original question with some more info. The data type is numeric/dbl for the "Allowed_Amount" column. – user12133051 Sep 29 '19 at 16:59