My problem is that I have data below(input table). But I want the table like the output table. So far I have not found a solution to get this done.
Inpunt table: Input table
Output table:Output table
Input dataset:
set.seed(1)
Data <- data.frame(
set = (1:10),
Topic = sample(1:5),
Label = sample(c("A", "B", "C"), 10, replace = TRUE),
Score = sample(1:10)
)
Data
set Topic Label Score
1 1 1 C 3
2 2 2 B 5
3 3 3 A 10
4 4 4 A 9
5 5 5 A 2
6 6 1 A 8
7 7 2 B 4
8 8 3 B 1
9 9 4 B 6
10 10 5 C 7
Output data:
#In the columns I want the Topic (T).
T1 T2 T3 T4 T5
Label A 1 0 1 1 1
Label B 0 2 1 1 0
Label C 1 0 0 0 1
Score (avg) 5.5 4.5 5.5 7.5 4.5
Set (count) 2 2 2 2 2
I have tried from tidyr the spread function but I get a lot of NA values and I get no numbers.
Data_1 <- spread(Data, key = Topic, value = Label