I am trying to transform a dataframe where I'd like multiple rows to be collapsed into a single column. I have pasted what the current data frame looks like, and what I want the final data frame to look, like further down.
The column "subject.number" is currently populated with a number from 1-9. The numbers are repeated multiple times depending on how many corresponding "sentiment.score" values that subject has.
I would like to end up with a data frame where each subject number is its own column, and that column is populated with its corresponding sentiment scores as rows.
Here is the original data frame
subject.number sentiment.score
1 1 -1.0
2 1 -0.5
3 1 0.0
4 1 0.5
5 1 1.0
6 1 -1.0
7 1 -0.5
8 1 0.0
9 1 0.5
10 1 1.0
11 2 -1.0
12 2 -0.5
13 2 0.0
14 2 0.5
15 2 1.0
16 2 -1.0
17 2 -0.5
18 2 0.0
19 2 0.5
20 3 1.0
21 3 -1.0
22 3 -0.5
23 3 0.0
24 3 0.5
25 3 1.0
26 3 -1.0
27 3 -0.5
28 4 0.0
29 4 0.5
30 4 1.0
31 4 -1.0
32 4 -0.5
33 4 0.0
34 4 0.5
35 5 1.0
36 5 -1.0
37 5 -0.5
38 5 0.0
39 5 0.5
40 5 1.0
41 6 -1.0
42 6 -0.5
43 6 0.0
44 6 0.5
45 6 1.0
46 7 -1.0
47 7 -0.5
48 7 0.0
49 7 0.5
50 8 1.0
51 8 -1.0
52 8 -0.5
53 9 0.0
54 9 0.5
Here is what I want the final data frame to look like
X1 X2 X3 X4 X5 X6 X7 X8 X9
1 -1.0 -1.0 1.0 0.0 1.0 -1.0 -1.0 1.0 0.0
2 -0.5 -0.5 -1.0 0.5 -1.0 -0.5 -0.5 -1.0 0.5
3 0.0 0.0 -0.5 1.0 -0.5 0.0 0.0 -0.5 NA
4 0.5 0.5 0.0 -1.0 0.0 0.5 0.5 NA NA
5 1.0 1.0 0.5 -0.5 0.5 1.0 NA NA NA
6 -1.0 -1.0 1.0 0.0 1.0 NA NA NA NA
7 -0.5 -0.5 -1.0 0.5 NA NA NA NA NA
8 0.0 0.0 -0.5 NA NA NA NA NA NA
9 0.5 0.5 NA NA NA NA NA NA NA
10 1.0 NA NA NA NA NA NA NA NA
Any help would be greatly appreciated. I thought maybe constructing a for loop could be good to solve this problem? But am unsure how to format it.
Also please excuse any weird formatting in my question - this is my first time posting on stackexchange so I'm still getting used to it.