Consider this code:
sample(1:20, 1)
If the result happens to be less than or equal to 10, can I get R to print out the number as a word. For example, if the result of sample(1:20, 1)
is 2, can I program R to print the result as two, if the result of sample(1:20, 1)
is 10, can I program R to print the result as ten, if the result of sample(1:20, 1)
is 13, can I program R to print the result as 13, and so on.
I am using knitr
to convert R code to latex for my thesis. My rule is any number less than or equal to 10 should be printed as word.
You can use the english
package to transform numbers into English words:
set.seed(1)
s <- sample(1:20, 10)
# [1] 6 8 11 16 4 14 15 9 19 1
library(english)
ifelse(s > 10, s, as.character(english(s)))
# [1] "six" "eight" "11" "16" "four" "14" "15" "nine" "19" "one"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With