1
我試圖排序因子變量類別,所述金額柱是數字,並且 單位是一個因素,其中(B =十億,M =百萬, K =千)排序的一個因素變量基於數字變量
d <- read.table(text="Category Amount Units
A 1.2 B
B 2.4 K
C 3.2 M
A 2.3 B
B 4.5 M
C 6.5 K", header=TRUE, stringsAsFactors = TRUE)
這裏是我做了什麼
d$Total <- ifelse(d$Units=="B", 1000000000, ifelse(d$Units =="M", 1000000, ifelse(d$Units == "K", 1000, 0)))
d$Total <- d$Total * Amount
arrange(d, desc(Amount), Units)
我的問題,有沒有acheiving它的更好的辦法?