I have a question about data.table meltand dcastfor multiple columns. I looked at StackOverFlow, but many of these posts are not what I want. I will explain below.
Firstly, it dataconcerns the causes of problems and the sum of the values. Here is my part 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
Secondly, IDis unique. Typecontains three ( A, Band C). There are problems 5 .
Take ID == 1as an example. This Type Acontains 3 tasks ( X, Yand Z). He Problem Xhas Value 500, Problem Yhas Value 1000, Problem Zhas Value 400. Take ID == 5as an example. It Type Ccontains two tasks ( Zand V). He Problem Zhas Value 500and Problem Vhas Value 500.
Third, column ID, Type, Problem1, Problem2and Problem3are character. Value1,, Value2and Value3- numeric.
As a result, I want:
Type X Y Z W V
A 1100 1000 1100 0 0
B 0 700 100 200 200
C 1000 0 500 100 1400
, . Type, vlaue. , . . . , . ?
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)