I have some data in long format that I want to make wide. The issue is that the data is in a format doesn't have one piece of information that makes spreading easy. To get around this, I have to create a column in the data frame that counts from 1 to the length of each unique entry in another column.
In the process below, is there a data.table way of making the column "fid"?
library(data.table)
library(tidyverse)
# data:
df <- data.frame(class = c('1', '1', '1', '2', '3', '3'),
A = 1:6,
B = 11:16)
# create counting column
df <- df %>% group_by(class) %>% mutate(fid=1:n())
# spread using dcast
dcast(setDT(df), class ~ fid, value.var = c("A", "B"))
Cheers