 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP 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
How to multiply row values in a data frame having multiple rows with single row data frame in R?
To multiply row values in a data frame having multiple rows with single row data frame in R, we can follow the below steps −
- First of all, create a data frame with multiple rows and a data frame with single row. 
- Then, use mapply function to multiply row values in the data frame having multiple rows with single row data frame. 
Example
Create the first data frame
Let’s create a data frame as shown below −
x<-rpois(25,5) y<-rpois(25,2) z<-rpois(25,2) df1<-data.frame(x,y,z) df1
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
x y z 1 8 4 0 2 8 2 1 3 2 1 2 4 4 1 4 5 6 5 1 6 4 2 1 7 6 1 3 8 4 3 3 9 3 1 2 10 3 3 1 11 6 0 3 12 7 1 2 13 3 4 5 14 7 2 4 15 2 2 2 16 5 6 2 17 2 0 2 18 5 2 0 19 4 1 3 20 5 2 1 21 3 3 1 22 4 0 1 23 5 3 2 24 4 2 4 25 5 1 2
Create the second data frame
Let’s create a data frame as shown below −
df2<-data.frame(v1=5,v2=0,v3=2) df2
Output
v1 v2 v3 1 5 0 2
Multiply values from two data frames
Using mapply function to multiply row values in the data frame df1 having multiple rows with single row data frame df2 −
x<-rpois(25,5) y<-rpois(25,2) z<-rpois(25,2) df1<-data.frame(x,y,z) df2<-data.frame(v1=5,v2=0,v3=2) mapply(`*`,df1,df2)
Output
x y z [1,] 25 0 8 [2,] 20 0 2 [3,] 50 0 6 [4,] 25 0 8 [5,] 10 0 8 [6,] 10 0 2 [7,] 30 0 4 [8,] 35 0 2 [9,] 40 0 4 [10,] 30 0 6 [11,] 30 0 0 [12,] 20 0 0 [13,] 20 0 2 [14,] 15 0 6 [15,] 25 0 0 [16,] 15 0 6 [17,] 10 0 0 [18,] 40 0 4 [19,] 35 0 4 [20,] 15 0 4 [21,] 15 0 0 [22,] 20 0 0 [23,] 30 0 2 [24,] 25 0 2 [25,] 35 0 6
Advertisements
 