I have a question about data.table's melt and dcast for multiple columns. I browsed on StackOverFlow but many similar posts are not what I want. I will explain below.
First, data is about causes of problems and the value amount. Here is my part of data:
ID Type Problem1 Value1 Problem2 Value2 Problem3 Value3
1 A X 500 Y 1000 Z 400
2 A X 600 Z 700
3 B Y 700 Z 100
4 B W 200 V 200
5 C Z 500 V 500
6 C X 1000 W 100 V 900
Second, ID is unique. Type contains three(A, B, and C). There are 5 problems.
Take ID == 1 as an example. It is Type A and it contains 3 problems(X, Y, and Z). Its Problem X has Value 500, Problem Y has Value 1000, Problem Z has Value 400. Take ID == 5 as an example. It is Type C and contains 2 problems(Z and V). Its Problem Z has Value 500 and Problem V has Value 500.
Third, column ID, Type, Problem1, Problem2, and Problem3 are character. Value1, Value2, and Value3 are numeric.
The result what I want is:
Type X Y Z W V
A 1100 1000 1100 0 0
B 0 700 100 200 200
C 1000 0 500 100 1400
I don't know how to explain here properly. I want to group the Type and then summation each problem's vlaue. I think this is about long to wide. I found reference here and here. Second one may be useful. However, I have no idea where to begin. Any suggestions?
# data
dt <- fread("
ID Type Problem1 Value1 Problem2 Value2 Problem3 Value3
1 A X 500 Y 1000 Z 400
2 A X 600 Z 700
3 B Y 700 Z 100
4 B W 200 V 200
5 C Z 500 V 500
6 C X 1000 W 100 V 900", fill = T)