I couldn't find the answer to this but maybe i looked with the wrong keywords, sorry in advance if that's the case. Basically i have a dataframe that looks like this:
code class Var1 Var2
1 U1 A 1 3
2 U2 A 5 43
3 U1 B 6 3
4 U2 B 6 12
5 U1 C 87 7
6 U2 C 1 8
It can be reproduced using the following code:
df <- data.frame(code=c("U1","U2","U1","U2","U1","U2"),class = c("A","A","B","B","C","C"), Var1 = c(1,5,6,6,87,1), Var2 = c(3,43,3,12,7,8))
And i would like to make it look like this by creating new columns based on the "Class" column modalities and the other columns, while keeping the column "Code" unchanged:
Code Var1.A Var2.A Var1.B Var2.B Var1.C Var2.C
1 U1 1 3 6 3 87 7
2 U2 5 43 6 12 1 8
This second dataframe can be reproduced using this line:
df2 <- data.frame(Code=c("U1","U2"),Var1.A = c(1,5), Var2.A = c(3,43), Var1.B = c(6,6),Var2.B=c(3,12),Var1.C=c(87,1),Var2.C=c(7,8))
Please note that my real dataframe is way longer than the one in this example, and i would probably need an automated solution
Thank you very much!