Watts-College / cpp-527-fall-2021

A course shell for CPP 527 Foundations of Data Science II
https://watts-college.github.io/cpp-527-fall-2021/
2 stars 6 forks source link

Dollarize #81

Open millmeli42 opened 2 years ago

millmeli42 commented 2 years ago

Hello Professor -

I was wondering if I could get a hint on using this function, it keeps turning it into a character class, instead of numeric and I get errors later on in the processing.

dollarize <- function(x)
{ paste0("$", format( round( x, 0 ), big.mark="," ) ) }

I am currently using it at the normalize salary stage.

I do get the function to work, it adds the $ and the , but just makes d$salary a character class.

Thanks!

lecy commented 2 years ago

Good news is you are using it correctly. It does in fact turn a numeric vector into a character vector. You are reversing what you did in Step 5 - changing a string into a numeric vector.

See: https://github.com/Watts-College/cpp-527-fall-2021/issues/75#issuecomment-938148773

You should use it before printing tables mostly so that the results are nicely formatted. But only after you don't need the salaries to be numbers anymore.

top_five <- function( d )
{
  d.top5 <-
    d %>%
    select(  ) %>% 
    arrange(  ) %>%
    slice( )

  # add formatting to numeric vector 
  d.top5$salary <- dollarize( d.top5$salary )

  return( d.top5 )
}

For example, when grabbing the top 5 salaries you need to sort salaries first. If you sort a character string you will get a different set of top 5 than if you sort a numeric string:

x <-
c("$35,090.00", "$71,400.00", "$36,000.00", "$64,000.00", "$20,800.00", 
"$107,195.00", "$147,225.00", "$98,426.00", "$89,406.00", "$95,095.00" )

> sort( x, decreasing=TRUE )
 [1] "$98,426.00"  "$95,095.00"  "$89,406.00"  "$71,400.00"  "$64,000.00" 
 [6] "$36,000.00"  "$35,090.00"  "$20,800.00"  "$147,225.00" "$107,195.00"
millmeli42 commented 2 years ago

Thank you - sorry I missed this on the other post!!

lecy commented 2 years ago

There are a lot of threads...