Thank you andresrcs,
I didn't know library(read.so ) but I would like to show another example which I do not fully understand.
What is happening here:
Example from this link:
https://stackoverflow.com/questions/9341865/alternatives-to-statsreshape
library(read.so)
library(stats)
tmp <- read_so(
"id val val2 cat
1 1 14 a
1 2 13 b
2 3 12 b
2 4 11 a")
tmp2 <- tmp
tmp2$t <- ave(tmp2$val, tmp2$id, FUN=seq_along)
stats::reshape(tmp2, idvar= "id", timevar = "t", direction = "wide")
#> # A tibble: 2 × 4
#> id `val.c(1, 2)` `val2.c(1, 2)` `cat.c(1, 2)`
#> <dbl> <dbl> <dbl> <chr>
#> 1 1 NA NA <NA>
#> 2 2 NA NA <NA>
but when I convert tmp2 to data.table object:
``` r
library(data.table)
library(read.so)
library(stats)
tmp <- read_so(
"id val val2 cat
1 1 14 a
1 2 13 b
2 3 12 b
2 4 11 a")
tmp2 <- tmp
tmp2$t <- ave(tmp2$val, tmp2$id, FUN=seq_along)
tmp2dt <- setDT(tmp2)
class(tmp2)
#> [1] "data.table" "data.frame"
stats::reshape(tmp2dt, idvar= "id", timevar = "t", direction = "wide")
#> id val.1 val2.1 cat.1 val.2 val2.2 cat.2
#> 1: 1 1 14 a 2 13 b
#> 2: 2 3 12 b 4 11 a
Created on 2022-06-19 by the reprex package (v2.0.1)
which gives us completely different results:
Additionally what is interesting here, when I create tmp2, its class is as follows:
class(tmp2)
[1] "spec_tbl_df" "tbl_df" "tbl" "data.frame"
then when I convert it to data.table object named tmp2dt suddenly tmp2 becomes data.table object as well (allowing for correct calculations with tmp2 as well):
tmp2dt <- setDT(tmp2)
>class(tmp2)
[1] "data.table" "data.frame"
so I would like to kindly ask why is that ?