R spread dataframe [duplicate]
问题 This question already has answers here : Reshape multiple value columns to wide format (5 answers) Closed 7 months ago . IN R language how to convert data1 into data2 data1 = fread(" id year cost pf loss A 2019-02 155 10 41 B 2019-03 165 14 22 B 2019-01 185 34 56 C 2019-02 350 50 0 A 2019-01 310 40 99") data2 = fread(" id item 2019-01 2019-02 2019-03 A cost 30 155 NA A pf 40 10 NA A loss 99 41 NA B cost 185 NA 160 B pf 34 NA 14 B loss 56 NA 22 C cost NA 350 NA C pf NA 50 NA C loss NA 0 NA") I