This is a straightforward "reshape" problem. In base R, you could do:
reshape(prop_attack, direction = "wide", idvar="attack.type", timevar="class")
# attack.type proportion.high proportion.mid
# 1 4 0.8400000 0.6666667
# 2 5 0.9733333 0.9965636
# 3 6 0.9385151 0.9687825
# 4 7 0.9228659 NA
# 5 8 0.6187500 NA
# 6 9 0.9219331 NA
# 7 1 NA 0.8364853
# 8 2 NA 0.9896870
# 9 3 NA 0.9529760
Or even use xtabs
:
xtabs(proportion ~ attack.type + class, prop_attack)
# class
# attack.type high mid
# 1 0.0000000 0.8364853
# 2 0.0000000 0.9896870
# 3 0.0000000 0.9529760
# 4 0.8400000 0.6666667
# 5 0.9733333 0.9965636
# 6 0.9385151 0.9687825
# 7 0.9228659 0.0000000
# 8 0.6187500 0.0000000
# 9 0.9219331 0.0000000
Using a package, many would suggest dcast
from "reshape2" for its convenient syntax:
dcast(prop_attack, attack.type ~ class, value.var="proportion")
# attack.type high mid
# 1 1 NA 0.8364853
# 2 2 NA 0.9896870
# 3 3 NA 0.9529760
# 4 4 0.8400000 0.6666667
# 5 5 0.9733333 0.9965636
# 6 6 0.9385151 0.9687825
# 7 7 0.9228659 NA
# 8 8 0.6187500 NA
# 9 9 0.9219331 NA