If I have DF
A B Col
1 1 A
2 2 B
1 2 C
2 1 D
1 3 E
2 3 F
I try to use sqldf as follows
Test <- sqldf(" SELECT A,
case when A = '1' and B = '1' then Col else NULL end as Test_1,
case when A = '1' and B = '2' then Col else NULL end as Test_2,
case when A = '1' and B = '3' then Col else NULL end as Test_3,
case when A = '2' and B = '1' then Col else NULL end as Test_4,
case when A = '2' and B = '2' then Col else NULL end as Test_5,
case when A = '2' and B = '3' then Col else NULL end as Test_6
FROM DF
group by A;")
However, it only returns result for each case of A and the other columns NULL so instead of my desired
A Test1 Test2 Test3 Test4 Test5 Test6
1 A C E Null NULL NULL
2 NULL NULL NULL D B F
I only get one result for each A
A Test1 Test2 Test3 Test4 Test5 Test6
1 G NULL NULL Null NULL NULL
2 NULL NULL NULL NULL B NULL
What am I doing wrong and is there a way to get the format that I require?