I'm losing the row names of the data. It appears to happen when the rbind function is used on a data table.
Here's an example showing what should happen
library(data.table)
allData <- NULL
for (itest in seq(3)) {
pts <- NULL
npts <- 4
for (ipt in seq(npts)) {
pp <- c(ipt, ipt*2, ipt^3)
pts <- rbind(pts,pp)
}
colnames(pts)<-c('A','B','C')
rownames(pts) <- paste('test',itest,seq(npts),sep='_')
# pts<-data.table(pts)
print(pts)
allData <- rbind(allData,pts)
}
print(allData)
The output is
A B C
test_1_1 1 2 1
test_1_2 2 4 8
test_1_3 3 6 27
test_1_4 4 8 64
test_2_1 1 2 1
test_2_2 2 4 8
test_2_3 3 6 27
test_2_4 4 8 64
test_3_1 1 2 1
test_3_2 2 4 8
test_3_3 3 6 27
test_3_4 4 8 64
When the data table is used, the row names are lost
library(data.table)
allData <- NULL
for (itest in seq(3)) {
pts <- NULL
npts <- 4
for (ipt in seq(npts)) {
pp <- c(ipt, ipt*2, ipt^3)
pts <- rbind(pts,pp)
}
colnames(pts)<-c('A','B','C')
pts<-data.table(pts)
rownames(pts) <- paste('test',itest,seq(npts),sep='_')
allData <- rbind(allData,pts)
}
print(allData)
Output with data table
A B C
1: 1 2 1
2: 2 4 8
3: 3 6 27
4: 4 8 64
5: 1 2 1
6: 2 4 8
7: 3 6 27
8: 4 8 64
9: 1 2 1
10: 2 4 8
11: 3 6 27
12: 4 8 64
How should the code be modified to keep the row names?