I have a dataframe as below:
structure(list(Value = c(1, 2, 3, 4), col1 = structure(c(1L,
1L, 2L, 2L), .Label = c("A1", "A2"), class = "factor"), col2 = structure(c(1L,
2L, 2L, 1L), .Label = c("B1", "B2"), class = "factor"), col3 = structure(1:4, .Label = c("C1",
"C2", "C3", "C4"), class = "factor")), class = "data.frame", row.names = c(NA,
-4L))
I want to spread unique values in each column to different columns using data.table and paste the summed value (from column 'Value') under each column For ex: Column col1 has 2 unique values A1 and A2. Sum of A1 is 3 and A2 is 7 Similarly, Column col2 has 2 unique values B1 and B2. Sum of B1 is 5 and B2 is 5
This operation will be performed for each of columns col1, col2 and col3.
The expected output is as below
structure(list(A1 = 3, A2 = 7, B1 = 5, B2 = 5, C1 = 1, C2 = 2,
C3 = 3, C4 = 4), class = "data.frame", row.names = c(NA,
-1L))
How can I achieve this in R ?