I have the follow type of table:
| SEZ | Class | Val |
|---|---|---|
| 1_1_1 | 1 | 2 |
| 1_1_1 | 5 | 2 |
| 1_1_2 | 5 | 2 |
| 1_1_3 | 1 | 1 |
| 1_1_3 | 5 | 2 |
| 1_1_4 | 1 | 1 |
| 1_1_5 | 2 | 1 |
| 1_2_1 | 1 | 2 |
| 1_2_1 | 5 | 2 |
In order to spread column "Class" in multiple new columns getting value from "Val" column I used pivot_wider and everything gone well. I entered this code:
pivot_wider(names_from = Class, values_from = Val, names_sort=T, values_fill = list(n = 0))
obataining a result like this:
| Sez | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| 1_1_1 | 2 | 0 | 0 | 0 | 2 | 0 | 0 |
| 1_1_2 | 0 | 0 | 0 | 0 | 2 | 0 | 0 |
| 1_1_3 | 1 | 0 | 0 | 0 | 2 | 0 | 0 |
Unfortunately I have to use an external computer where only the basic R packages are present, and the times for requesting additional packages are not short.
I tried to use this solution:
newdata <- xtabs(dat$Val ~ dat$Sez + dat$Class)
But it gives me a frequency distribution with each row:
| SEZ | Class | Freq |
|---|---|---|
| 1_1_1 | 1 | 2 |
| 1_1_2 | 1 | 0 |
| 1_1_3 | 1 | 1 |
| 1_1_4 | 1 | 1 |
| 1_1_5 | 1 | 0 |
| 1_2_1 | 1 | 1 |
I'm looking for a solution with the basic function of R that gives me an object equal to the one got using with pivot_wider.