I am trying to create the product of columns of a dataset and a vector. The dataset is this:
col1<-rep(c(1:4),3)
col2<-rep(c(1:4),3)
col3<-rep(c(1:4),3)
col4<-rep(c(1:4),3)
df<-data.table(col1,col2,col3,col4)
and the vector this
w<-data.table(w<-c(100,0.1,0.2))
for (i in 1:nrow(w))
{
new[i]<-df[,as.integer(i+1)]*w[as.integer(i)]}
The dataset I want is this
col1 col2 col3 col4
1 100 0.1 0.2
2 200 0.2 0.4
3 300 0.3 0.6
4 400 0.4 0.8
1 100 0.1 0.2
2 200 0.2 0.4
3 300 0.3 0.6
4 400 0.4 0.8
1 100 0.1 0.2
2 200 0.2 0.4
3 300 0.3 0.6
4 400 0.4 0.8
what i get instead is this
col1 col2 col3 col4
200 200 200 200
0 0 0 0
1 1 1 1
4 4 4 4
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
What am I missing????