
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Multiply Vector Values with Data Frame Columns in R
To multiply vector values in sequence with data frame columns in R, we can follow the below steps −
First of all, create a data frame.
Then, create a vector.
After that, use t function for transpose and multiplication sign * to multiply vector values in sequence with data frame columns.
Example 1
Create the data frame
Let’s create a data frame as shown below −
x1<-rpois(25,2) x2<-rpois(25,2) x3<-rpois(25,2) df1<-data.frame(x1,x2,x3) df1
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
x1 x2 x3 1 3 2 1 2 1 0 3 3 1 1 2 4 2 1 1 5 5 1 0 6 4 2 3 7 1 2 3 8 1 2 2 9 2 1 0 10 0 1 2 11 0 2 3 12 1 1 2 13 4 3 0 14 0 3 0 15 4 5 2 16 4 2 2 17 5 4 5 18 1 3 1 19 1 3 4 20 1 0 5 21 3 4 1 22 4 0 0 23 1 2 2 24 2 2 1 25 1 0 1
Create the vector
Let’s create a vector as shown below −
v1<-c(1,5,10) v1
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
[1] 1 5 10
Multiply vector values in sequence with data frame columns
Using t function for transpose and multiplication sign * to multiply v1 values in sequence with columns of data frame df1 as shown below −
x1<-rpois(25,2) x2<-rpois(25,2) x3<-rpois(25,2) df1<-data.frame(x1,x2,x3) v1<-c(1,5,10) t(t(df1)*v1)
Output
x1 x2 x3 [1,] 1 20 10 [2,] 2 10 10 [3,] 2 5 40 [4,] 4 30 20 [5,] 1 30 30 [6,] 3 0 10 [7,] 2 0 10 [8,] 1 20 40 [9,] 1 15 50 [10,] 1 0 10 [11,] 3 25 30 [12,] 0 15 0 [13,] 1 25 40 [14,] 3 5 30 [15,] 3 10 0 [16,] 2 10 10 [17,] 2 10 20 [18,] 0 15 10 [19,] 1 15 30 [20,] 3 5 50 [21,] 1 10 10 [22,] 0 20 40 [23,] 1 25 10 [24,] 1 5 0 [25,] 3 10 0
Example 2
Create the data frame
Let’s create a data frame as shown below −
y1<-round(rnorm(25),2) y2<-round(rnorm(25),2) y3<-round(rnorm(25),2) df2<-data.frame(y1,y2,y3) df2
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
y1 y2 y3 1 -0.20 -0.19 1.14 2 -0.04 -0.99 2.09 3 0.47 0.83 0.49 4 -0.23 -0.24 0.18 5 0.02 -1.03 -2.62 6 1.52 -0.99 -1.16 7 -0.17 -0.38 1.87 8 0.64 -0.16 0.62 9 1.32 0.02 -1.07 10 -0.37 2.05 0.52 11 0.25 -0.29 1.19 12 0.69 -0.58 0.93 13 1.50 -0.34 1.32 14 1.06 -1.27 1.04 15 0.16 0.87 0.84 16 -0.81 0.31 0.11 17 1.50 -0.53 1.95 18 -1.27 2.88 -0.88 19 0.37 -0.14 2.47 20 1.36 -1.51 1.36 21 1.29 -0.40 -0.24 22 -1.12 0.99 1.44 23 -0.17 -1.07 -0.22 24 -0.95 1.71 -0.46 25 0.00 -0.63 1.02
Create the vector
Let’s create a vector as shown below −
v2<-c(10,20,10) v2
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
[1] 10 20 10
Multiply vector values in sequence with data frame columns
Using t function for transpose and multiplication sign * to multiply v2 values in sequence with columns of data frame df2 as shown below −
y1<-round(rnorm(25),2) y2<-round(rnorm(25),2) y3<-round(rnorm(25),2) df2<-data.frame(y1,y2,y3) v2<-c(10,20,10) t(t(df2)*v2)
Output
y1 y2 y3 [1,] -2.0 -3.8 11.4 [2,] -0.4 -19.8 20.9 [3,] 4.7 16.6 4.9 [4,] -2.3 -4.8 1.8 [5,] 0.2 -20.6 -26.2 [6,] 15.2 -19.8 -11.6 [7,] -1.7 -7.6 18.7 [8,] 6.4 -3.2 6.2 [9,] 13.2 0.4 -10.7 [10,] -3.7 41.0 5.2 [11,] 2.5 -5.8 11.9 [12,] 6.9 -11.6 9.3 [13,] 15.0 -6.8 13.2 [14,] 10.6 -25.4 10.4 [15,] 1.6 17.4 8.4 [16,] -8.1 6.2 1.1 [17,] 15.0 -10.6 19.5 [18,] -12.7 57.6 -8.8 [19,] 3.7 -2.8 24.7 [20,] 13.6 -30.2 13.6 [21,] 12.9 -8.0 -2.4 [22,] -11.2 19.8 14.4 [23,] -1.7 -21.4 -2.2 [24,] -9.5 34.2 -4.6 [25,] 0.0 -12.6 10.2