2016-08-11 49 views
1

我希望打印米平方在R,但我沒有成功。 這是我正在使用的代碼示例:打印米在R平方

bmi <- c(24,28,31) 
print(paste0("Your body mass index is ", bmi, "kg/m^2")) 
# [1] "Your body mass index is 24kg/m^2" 
# [2] "Your body mass index is 28kg/m^2" 
# [3] "Your body mass index is 31kg/m^2" 

建議?

+1

使用字符'²'? – AlexR

+0

謝謝@AlexR –

+0

如果我的回答對您有幫助,請接受它,以便其他人知道您不再需要該問題的答案,並且它從未答覆的列表中消失。 – AlexR

回答

3

您可以使用UTF-8字符「SUPERSCRIPT TWO」,²(see here)。 如果你正確編碼你的R腳本(或直接進入終端),它會工作得很好。

如果沒有這個選項(或您無法鍵入這封信),使用\u轉義序列:\u00B2

bmi <- c(24,28,31) 
cat(paste0("Your body mass index is ", bmi, "kg/m²"), sep = "\n") 
cat(paste0("Your body mass index is ", bmi, "kg/m\u00B2"), sep = "\n") 
# Your body mass index is 24kg/m² 
# Your body mass index is 28kg/m² 
# Your body mass index is 31kg/m² 

較新的R版本中允許您與source指定文件編碼,所以這將是

source("myFancyScript.R", encoding = "UTF-8")