- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to multiply vector values in sequence with columns of a data.table object in R?
To multiply vector values in sequence with data.table object columns in R, we can follow the below steps:−
First of all, create a data.table.
Then, create a vector.
After that, use t function for transpose and multiplication sign * to multiply vector values in sequence with data.table object columns.
Example 1
Create the data.table object
Let’s create a data.table object as shown below −
library(data.table) var1<-sample(1:10,25,replace=TRUE) var2<-sample(1:10,25,replace=TRUE) var3<-sample(1:10,25,replace=TRUE) DT1<-data.table(var1,var2,var3) DT1
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
var1 var2 var3 1: 9 8 4 2: 2 8 2 3: 7 8 10 4: 6 7 10 5: 2 5 9 6: 4 9 8 7: 8 1 6 8: 3 9 9 9: 10 2 4 10: 5 7 8 11: 2 5 7 12: 10 6 2 13: 9 3 9 14: 8 3 10 15: 4 3 9 16: 2 6 4 17: 4 10 4 18: 10 6 5 19: 2 9 5 20: 9 9 5 21: 7 8 1 22: 5 3 5 23: 6 10 4 24: 5 6 10 25: 2 1 5 var1 var2 var3
Create the vector
Let’s create a vector as shown below −
v1<-c(10,10,10) v1
Output
[1] 10 10 10
Multiply vector values in sequence with data.table object columns
Using t function for transpose and multiplication sign * to multiply v1 values in sequence with columns of data.table object DT1 as shown below −
library(data.table) var1<-sample(1:10,25,replace=TRUE) var2<-sample(1:10,25,replace=TRUE) var3<-sample(1:10,25,replace=TRUE) DT1<-data.table(var1,var2,var3) v1<-c(10,10,10) t(t(DT1)*v1)
Output
var1 var2 var3 [1,] 90 80 40 [2,] 20 80 20 [3,] 70 80 100 [4,] 60 70 100 [5,] 20 50 90 [6,] 40 90 80 [7,] 80 10 60 [8,] 30 90 90 [9,] 100 20 40 [10,] 50 70 80 [11,] 20 50 70 [12,] 100 60 20 [13,] 90 30 90 [14,] 80 30 100 [15,] 40 30 90 [16,] 20 60 40 [17,] 40 100 40 [18,] 100 60 50 [19,] 20 90 50 [20,] 90 90 50 [21,] 70 80 10 [22,] 50 30 50 [23,] 60 100 40 [24,] 50 60 100 [25,] 20 10 50
Example 2
Create the data.table object
Let’s create a data.table object as shown below −
library(data.table) x<-sample(1:50,25) y<-sample(1:50,25) z<-sample(1:50,25) DT2<-data.table(x,y,z) DT2
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
x y z 1: 3 33 8 2 35 25 43 3: 9 22 36 4: 17 26 27 5: 32 7 29 6: 4 23 13 7: 31 5 37 8: 1 20 41 9: 50 17 20 10: 37 30 25 11: 44 10 4 12: 43 42 42 13: 2 18 3 14: 33 37 22 15: 36 32 15 16: 49 2 26 17: 39 48 33 18: 23 24 11 19: 26 29 34 20: 48 8 14 21: 40 35 9 22: 22 36 1 23: 12 14 40 24: 19 12 10 25: 13 9 7 x y z
Create the vector
Let’s create a vector as shown below −
v2<-c(1,0.5,2) v2
Output
[1] 1.0 0.5 2.0
Multiply vector values in sequence with data.table object columns
Using t function for transpose and multiplication sign * to multiply v2 values in sequence with columns of data.table object DT2 as shown below −
library(data.table) x<-sample(1:50,25) y<-sample(1:50,25) z<-sample(1:50,25) DT2<-data.table(x,y,z) v2<-c(1,0.5,2) t(t(DT2)*v2)
Output
x y z [1,] 3 16.5 16 [2,] 35 12.5 86 [3,] 9 11.0 72 [4,] 17 13.0 54 [5,] 32 3.5 58 [6,] 4 11.5 26 [7,] 31 2.5 74 [8,] 1 10.0 82 [9,] 50 8.5 40 [10,] 37 15.0 50 [11,] 44 5.0 8 [12,] 43 21.0 84 [13,] 2 9.0 6 [14,] 33 18.5 44 [15,] 36 16.0 30 [16,] 49 1.0 52 [17,] 39 24.0 66 [18,] 23 12.0 22 [19,] 26 14.5 68 [20,] 48 4.0 28 [21,] 40 17.5 18 [22,] 22 18.0 2 [23,] 12 7.0 80 [24,] 19 6.0 20 [25,] 13 4.5 14
- Related Articles
- How to multiply vector values in sequence with matrix columns in R?
- How to multiply vector values in sequence with columns of a data frame in R?
- How to multiply corresponding row values in a data.table object with single row data.table object in R?
- How to randomize column values of a data.table object for all columns in R?
- How to randomize column values of a data.table object for a set of columns in R?
- How to extract data.table columns using a vector of column numbers in R?
- How to combine two columns of a data.table object in R?
- How to match the names of a vector in sequence with string vector values in another vector having same values in R?
- How to standardize selected columns in data.table object in R?
- How to scale some columns in data.table object in R?
- How to multiply corresponding values from two data.table objects in R?
- How to multiply a matrix with a vector in R?
- How to change data.table object columns value to maximum in R?
- How to create vector in R with a sequence of numbers?
- How to divide data.table object rows by number of columns in R?
