0

I have data that looks like this, df1:

Product Relative_Value
Car     0.12651458
Plane   0.08888552
Tank    0.03546231
Bike    0.06711630
Train   0.06382191

Relative_Value is the percentage of the total sell of the product. I use stargazer(df1, summary=FALSE, rownames=TRUE) to get latex code to make a nice table of the data. But how do I get either R or TeXstudio to understand that Relative_Value should be formatted as percentage and not decimals?

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
KGB91
  • 630
  • 2
  • 6
  • 24

1 Answers1

2

You can use percent() from the scales package

library(scales)

df1 <- df1 %>%
    mutate(Relative_Value = percent(Relative_Value))

Note that percent() changes the column type to character

Randall Helms
  • 849
  • 5
  • 15
  • When I run this on my df I get `Error in mutate_impl(.data, dots) : Evaluation error: non-numeric argument to mathematical function.` – KGB91 Oct 12 '18 at 09:33
  • 1
    Is Relative_Value a non-numeric column, perhaps by accident? Add as.numeric() to be sure, like this: `mutate(Relative_Value = percent(as.numeric(Relative_Value)))` – Randall Helms Oct 12 '18 at 09:39
  • Yeah, I just tested it by changing Relative_Value to character (from numeric) before putting it through percent() and I got the same error as you did. That's your issue. – Randall Helms Oct 12 '18 at 09:42
  • That was the problem it seems! Thanks :) (in the real code I added a row at the bottom to sum the Relative_Value for each df I have) – KGB91 Oct 12 '18 at 09:43