I am having my very first experience with data tables. I have a data table "dt" created using:
dt <- setNames(data.table(matrix(rnorm(10), nrow = 20, ncol = 5)), c("A",
"B","C", "D", "E"))
dt>
A B C D E
1: -1.6288978 -1.6288978 -1.6288978 -1.6288978 -1.6288978
2: -1.7256894 -1.7256894 -1.7256894 -1.7256894 -1.7256894
3: 0.2317219 0.2317219 0.2317219 0.2317219 0.2317219
4: -0.1146546 -0.1146546 -0.1146546 -0.1146546 -0.1146546
5: 0.3017276 0.3017276 0.3017276 0.3017276 0.3017276
6: -0.9280297 -0.9280297 -0.9280297 -0.9280297 -0.9280297
7: 1.3672226 1.3672226 1.3672226 1.3672226 1.3672226
8: 0.7097482 0.7097482 0.7097482 0.7097482 0.7097482
9: -1.3607799 -1.3607799 -1.3607799 -1.3607799 -1.3607799
10: -0.6886022 -0.6886022 -0.6886022 -0.6886022 -0.6886022
I want to iteratively read elements of this data table "dt" into the first and second columns of an empty data table "dt1" with 10 rows and two columns.
dt1 <- setNames(data.table(matrix(NA, nrow = 10, ncol = 2)), c("X", "Y"))
X Y
1: NA NA
2: NA NA
3: NA NA
4: NA NA
5: NA NA
6: NA NA
7: NA NA
8: NA NA
9: NA NA
10: NA NA
Like I will do for a data frame, I wrote the following nested for loop to iteratively access elements from data frame dt and put into the relevant columns of "dt1".
But my code generates the following error:
j (the 2nd argument inside [...]) is a single symbol but column name 'k' is not found. Perhaps you intended DT[, ..k]. This difference to data.frame is deliberate and explained in FAQ 1.1.
I have read through FAQ 1.1. but I am not able to get a clear solution. Can any one give a hint on how to iteratively access each element of of the data table so I can complete the task? Thank you in advance.
What I have tried:
<pre>co <- 2
ro <- 10
for(k in 1:co){
for (i in 1:ro) {
dt1[i,1]<-dt[i,k]
dt1[i,2]<-dt[i,"E"]
}
}