I'm working mainly off of this post to conditionally format some numbers I have in two columns. Here's a dataframe:
d <- data.frame(ID = c("a","b","c"),
total = c(145000000, 9.5, 867455),
se = c(9000000,0.84,120835),stringsAsFactors=FALSE)
I'm trying to apply the suffix "M" for numbers above 100,000 but leave smaller numbers alone for this formatting.
From that earlier post, I've tried to adapt the solution like so:
d[,2:3] <- ifelse(d[,2:3] > 100000, paste(round(d[,2:3] / 1e6, 1), trim = TRUE), "M")
But either nothing happens when I run the code (d
remains unchanged) or I get this error: non-numeric argument to binary operator
For reference, I'd like the dataframe to look like this:
d <- data.frame(ID = c("a","b","c"),
total = c("1.45M", 9.5, "0.867M"),
se = c("9.0M",0.84,"0.12M"),stringsAsFactors=FALSE)
I don't mind if the results are character-formatted, as this is going to be output to a table in LaTeX and I won't be doing operations on these numbers.
Thanks!