Reputation: 607
I have a sparse vector/column in data table that I am using as a feature which has mostly zero's and then a few non zero elements. I wanted to scale/normalize the vector, but only for the non zero entries.
Any thoughts how to efficiently implement this.
Thanks in advance
c(0,0,0,0,0,0,0,10,20,5,0,0,0,0,0,0,5,3,1,30,0,0,0,0,0,0,0,1,1,0,0,0,0,0)
returns 0's and then I guess the abosolute value of the scaled numbers
Upvotes: 1
Views: 611
Reputation: 887048
We create a logical index ('i1') and scale
the vector based on it.
i1 <- v1!=0
v1[i1] <- scale(v1[i1])
v1 <- c(0,0,0,0,0,0,0,10,20,5,0,0,0,0,0,0,5,3,1,30,0,0,0,0,0,0,0,1,1,0,0,0,0,0)
Upvotes: 3