Reputation: 399
I have data (prop_attack), a portion of which, looks like this
attack.type proportion class
4 0.8400000 high
5 0.9733333 high
6 0.9385151 high
7 0.9228659 high
8 0.6187500 high
9 0.9219331 high
1 0.8364853 mid
2 0.9896870 mid
3 0.9529760 mid
4 0.6666667 mid
5 0.9965636 mid
6 0.9687825 mid
attack.type is actually categorical, they are just being assigned numbers 1-9. I want to create a table that rearranges the data such that
weap.type high mid
1 corresponding proportion value corresponding proportion value
2 corresponding proportion value corresponding proportion value
3 corresponding proportion value corresponding proportion value
4 corresponding proportion value corresponding proportion value
5 corresponding proportion value corresponding proportion value
6 etc.
7
8
9
Any suggestions on how to do this?
Upvotes: 0
Views: 104
Reputation: 193507
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
Upvotes: 2