I want to convert the long to wide format of my data using data.table. Normally I use unstack()
, but I don't know how to do it in data.table. Below is an example. As output I expect three columns and three rows.
library(data.table)
set.seed(1)
df <- data.frame(class = factor(rep(c("A", "B", "C"), times = 3)),
value = runif(9))
unstack(df, form = value ~ class)
#> A B C
#> 1 0.2655087 0.3721239 0.5728534
#> 2 0.9082078 0.2016819 0.8983897
#> 3 0.9446753 0.6607978 0.6291140
dt <- data.table(df)
dcast(dt, formula = value ~ class, value.var = "value")
#> value A B C
#> 1: 0.2016819 NA 0.2016819 NA
#> 2: 0.2655087 0.2655087 NA NA
#> 3: 0.3721239 NA 0.3721239 NA
#> 4: 0.5728534 NA NA 0.5728534
#> 5: 0.6291140 NA NA 0.6291140
#> 6: 0.6607978 NA 0.6607978 NA
#> 7: 0.8983897 NA NA 0.8983897
#> 8: 0.9082078 0.9082078 NA NA
#> 9: 0.9446753 0.9446753 NA NA
Additionally, I don't want to use an aggregate function, but in data.table on real data I see: Aggregate function missing, defaulting to 'length'
, so there are fewer rows in the result.
CodePudding user response:
I think this is what you want:
dcast(dt, rowid(class) ~ class)
# class A B C
#1: 1 0.2655087 0.3721239 0.5728534
#2: 2 0.9082078 0.2016819 0.8983897
#3: 3 0.9446753 0.6607978 0.6291140
https://stackoverflow.com/a/52501268/10276092
CodePudding user response:
We can try split
within dt
like below (but this option is available only if we have the same number of values per class, thus, the dcast
option by @M.Viking is recommended for general cases)
> dt[, split(value, class)]
A B C
1: 0.2655087 0.3721239 0.5728534
2: 0.9082078 0.2016819 0.8983897
3: 0.9446753 0.6607978 0.6291140