So I have this riddle for all R aficionados out there:
library(data.table)
set.seed(666)
res<-data.table(NULL)
for(i in 1:10){
res<-rbind(res,data.table(a=i,b=paste0(letters[sample(1:i)],collapse = "")))
}
res<-res[sample(10)]
resulting in:
>res
a b
1: 1 a
2: 9 dhgcbeifa
3: 3 cba
4: 7 gcafdeb
5: 6 eacdfb
6: 8 dacbfehg
7: 10 fehjaigcbd
8: 4 dacb
9: 5 daecb
10: 2 ba
But case A
>t(apply(res,1,nchar))
a b
[1,] 2 1
[2,] 2 9
[3,] 2 3
[4,] 2 7
[5,] 2 6
[6,] 2 8
[7,] 2 10
[8,] 2 4
[9,] 2 5
[10,] 2 2
However case B
>res[,lapply(.SD, nchar)]
a b
1: 1 1
2: 1 9
3: 1 3
4: 1 7
5: 1 6
6: 1 8
7: 2 10
8: 1 4
9: 1 5
10: 1 2
My question is why the 2
in column a
in case A is wrong?