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!