這裏有兩種選擇:
library(tidyr)
spread(df, Type, Amount)
# To ordered.A ordered.B ordered.C
#1 Jeff $8 <NA> <NA>
#2 John <NA> $5 <NA>
#3 Smith $1 <NA> $4
或者
library(reshape2)
dcast(df, To ~ Type, value.var = "Amount")
# To A B C
#1 Jeff $8 <NA> <NA>
#2 John <NA> $5 <NA>
#3 Smith $1 <NA> $4
或者,如果你從要素到字符更改列類,你可以這樣做:
df$Amount <- as.character(df$Amount)
dcast(df, To ~ Type, value.var = "Amount", fill = "$0")
# To A B C
#1 Jeff $8 $0 $0
#2 John $0 $5 $0
#3 Smith $1 $0 $4
,同樣
spread(df, Type, Amount, fill = "$0")
# To A B C
#1 Jeff $8 $0 $0
#2 John $0 $5 $0
#3 Smith $1 $0 $4
注意:如果你離開了「金額」欄的因素,並嘗試使用fill = "$0"
你會得到這樣的錯誤消息:
Warning message: In [<-.factor
(*tmp*
, is.na(ordered), value = 0) : invalid factor level, NA generated
如果你想擺脫那些「$」的在金額欄,這樣就可以真正使用這些數字進行進一步的處理,這裏是你如何能做到這一點的dplyr/tidyr鏈:
library(dplyr)
library(tidyr)
df %>%
mutate(Amount = as.numeric(gsub("\\$", "", Amount))) %>%
spread(Type, Amount, fill = 0)
# To A B C
#1 Jeff 8 0 0
#2 John 0 5 0
#3 Smith 1 0 4
這個解決方案涉及到「重塑」數據框架,有一個基本的R'reshape'函數,但是如果你想學習dplyr家族工具,使用的包是'tidyr'(它與'dplyr'是互補的)。看看函數'spread()' – konvas 2014-12-05 08:49:57