- 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 create a column in an R data frame that contains the multiplication of two columns?
Sometimes we need the multiplication of two columns and create a new column so that the multiplication can be used further for analysis. For example, to calculate BMI we need mass and height and the height is squared, therefore, we would be needing the square of height. For this purpose, we can either multiply height with height or simply take the square both the ways work. Hence, if only have height column in an R data frame then we can multiply it with itself.
Example
Consider the below data frame −
> set.seed(957) > x<-rpois(20,1) > y<-rpois(20,5) > z<-rpois(20,3) > df<-data.frame(x,y,z) > df
Output
x y z 1 0 1 3 2 0 7 3 3 0 6 7 4 3 7 3 5 1 3 7 6 0 3 4 7 1 3 5 8 0 4 2 9 0 5 0 10 0 4 3 11 0 3 7 12 0 8 3 13 1 7 5 14 1 5 5 15 1 10 7 16 0 5 3 17 0 7 2 18 0 5 4 19 0 4 4 20 0 5 2
Creating new columns that has multiplication of different columns −
Example
> df$xy<-df$x*df$y > df
Output
x y z xy 1 0 1 3 0 2 0 7 3 0 3 0 6 7 0 4 3 7 3 21 5 1 3 7 3 6 0 3 4 0 7 1 3 5 3 8 0 4 2 0 9 0 5 0 0 10 0 4 3 0 11 0 3 7 0 12 0 8 3 0 13 1 7 5 7 14 1 5 5 5 15 1 10 7 10 16 0 5 3 0 17 0 7 2 0 18 0 5 4 0 19 0 4 4 0 20 0 5 2 0
Example
> df$xz<-df$x*df$z > df
Output
x y z xy xz 1 0 1 3 0 0 2 0 7 3 0 0 3 0 6 7 0 0 4 3 7 3 21 9 5 1 3 7 3 7 6 0 3 4 0 0 7 1 3 5 3 5 8 0 4 2 0 0 9 0 5 0 0 0 10 0 4 3 0 0 11 0 3 7 0 0 12 0 8 3 0 0 13 1 7 5 7 5 14 1 5 5 5 5 15 1 10 7 10 7 16 0 5 3 0 0 17 0 7 2 0 0 18 0 5 4 0 0 19 0 4 4 0 0 20 0 5 2 0 0
Example
> df$yz<-df$y*df$z > df
Output
x y z xy xz yz 1 0 1 3 0 0 3 2 0 7 3 0 0 21 3 0 6 7 0 0 42 4 3 7 3 21 9 21 5 1 3 7 3 7 21 6 0 3 4 0 0 12 7 1 3 5 3 5 15 8 0 4 2 0 0 8 9 0 5 0 0 0 0 10 0 4 3 0 0 12 11 0 3 7 0 0 21 12 0 8 3 0 0 24 13 1 7 5 7 5 35 14 1 5 5 5 5 25 15 1 10 7 10 7 70 16 0 5 3 0 0 15 17 0 7 2 0 0 14 18 0 5 4 0 0 20 19 0 4 4 0 0 16 20 0 5 2 0 0 10
- Related Articles
- How to create table of two factor columns in an R data frame?
- How to subset an R data frame by specifying columns that contains NA?
- How to remove rows that contains NA values in certain columns of an R data frame?
- How to create a lagged column in an R data frame?
- How to create a group column in an R data frame?
- How to find the mean of a numerical column by two categorical columns in an R data frame?
- How to create an ID column for the combination of values in multiple columns in R data frame?
- How to create histogram of all columns in an R data frame?
- How to replace NA values in columns of an R data frame form the mean of that column?
- How to remove a column from a data frame that contains same value in R?
- How to convert a data frame column to date that contains integer values in R?
- How to remove rows that contains coded missing value for all columns in an R data frame?
- How to add a column between columns or after last column in an R data frame?
- How to create a new data frame for the mean of rows of some columns from an R data frame?
- How to convert multiple columns into single column in an R data frame?

Advertisements